Search code examples
androidimageviewandroid-drawabledrawerlayout

Adding .png image to DrawerLayout gives error


I am trying to add a image to the top of a side menu (DrawerLayout) in .png format, i imported the image to "drawable" folder and now im calling the image to be shown by:

android:src="@drawable/icone"

icone is the name of the image, but i get the following error:

FATAL EXCEPTION: main
                                             Process: com.example.bugdroid.menuexe, PID: 5076
                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bugdroid.menuexe/com.example.bugdroid.menuexe.MainActivity}: android.view.InflateException: Binary XML file line #17: Binary XML file line #17: Error inflating class android.support.design.widget.NavigationView

I want to put the image here, on the white square:

Layout

Layout:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/nav_header_height"
    android:background="@drawable/side_nav_bar"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    android:orientation="vertical"
    android:gravity="bottom">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:src="@drawable/icone"
        android:id="@+id/imageView" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:text="Sintomas"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Duarte Andrade "
        android:id="@+id/textView" />

</LinearLayout>

Solution

  • Problem is not .png. Your image does not fit within the available memory. This means memory allocation crosses the heap limit or your process demand a amount of memory that crosses the heap limit. Given that you are working with limited memory, ideally you only want to load a lower resolution version in memory. The lower resolution version should match the size of the UI component that displays it. An image with a higher resolution does not provide any visible benefit, but still takes up precious memory and incurs additional performance overhead due to additional on the fly scaling.

    Solution Try using Glide Library. To use glide library you need to add this to your grandle dependencies: compile 'com.github.bumptech.glide:glide:3.7.0' and compile 'com.android.support:support-v4:19.1.0'

    repositories {
      mavenCentral() // jcenter() works as well because it pulls from Maven Central
    }
    
    dependencies {
      compile 'com.github.bumptech.glide:glide:3.7.0'
      compile 'com.android.support:support-v4:19.1.0'
    }
    

    Finally load your image:

    ImageView imageview = (ImageView) findViewById(R.id.imageView);
     Glide.with(this).load(R.drawable.icone).diskCacheStrategy(DiskCacheStrategy.SOURCE).into(imageView);
    

    To use Glide, read this

    If Glide doesn't work then try this Hope it helps.