Several ImageView defined src in xml, error java.lang.OutOfMemoryError:

-1

I have a FrameLayout that contains the list of courses with image of the respective courses, all defined in XML, but the error occurs.

There are several images, around 15, with about 1280 x 1024 each.

<ImageView
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:src="@drawable/acucar"
     android:adjustViewBounds="true"
     android:scaleType="centerCrop"/>
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.etec.etecapp, PID: 29787
java.lang.OutOfMemoryError: Failed to allocate a 33177612 byte allocation with 16770928 free bytes and 18MB until OOM
    at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080)
    at android.content.res.Resources.loadDrawableForCookie(Resources.java:2738)
    at android.content.res.Resources.loadDrawable(Resources.java:2643)
    at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
    at android.widget.ImageView.<init>(ImageView.java:152)
    at android.widget.ImageView.<init>(ImageView.java:140)
    at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:57)
    at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:53)
    at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
    at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:1008)
    at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:1067)
    at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
    at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:189)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:746)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
    at com.example.etec.etecapp.CursoTecnico.onCreateView(CursoTecnico.java:44)
    at android.support.v4.app.Fragment.performCreateView(Fragment.java:2074)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:758)
    at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1632)
    at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:637)
    at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
    at android.support.v4.view.ViewPager.populate(ViewPager.java:1237)
    at android.support.v4.view.ViewPager.populate(ViewPager.java:1085)
    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1611)
    at android.view.View.measure(View.java:18860)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5995)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
    at android.view.View.measure(View.java:18860)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5995)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
    at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
    at android.view.View.measure(View.java:18860)
    at android.view
    
asked by anonymous 13.01.2017 / 13:01

1 answer

3

OutOfMemoryError is the most common problem that occurs with Android when dealing with bitmaps . This error is triggered by the Java Virtual Machine (JVM) when an object can not be allocated due to lack of memory space and also, the garbage collector can not free some space.

At first you can add in your AndroidManifest.xml these lines android:hardwareAccelerated="false" , android:largeHeap ="true" that works in some situations. See below:

<application
    android:allowBackup="true"
    android:hardwareAccelerated="false"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

More details about efficient bitmap view (en) in the documentation.

You can also read efficient loading of large bitmaps , which will help you better in handling these files.

Note

Ideally you will be using ARGB_8888 for bitmap. This means that for each pixel 4 bytes are assigned (each for A, R, G, B). It means that when loading an image of 1280x1024 , as you said in the question, it takes 1280 * 1024 * 4 = 5242880 B = 5242 KB = 5 MB.

SOen Response

    
13.01.2017 / 13:09