Several Imageview defined src in xml, from java.lang.Outofmemoryerror error:

Asked

Viewed 165 times

-1

I have a FrameLayout which contains the list of courses with picture of the respective courses, all defined in XML, but 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
  • What size of the image?

  • Send the image, if possible.

  • 1280 x 1024 image will make a mistake. Have you tried using a bumptech image like Glide? It automatically manages the loading of images and prevents this out of memory error from occurring, regardless of image size.

  • The error has to do with the size of the images. Before thinking of using any api or any other device(android:largeHeap="true" for example), consider, if possible, using images with a smaller size.

1 answer

3

OutOfMemoryError is the most common problem that occurs on Android when dealing with bitmaps. This error is triggered by the Java Virtual Machine (JVM) when an object cannot be allocated due to lack of memory space and also, the garbage collector cannot free up 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">

Worth reading more details about efficient display of bitmap in the documentation.

You can also read about efficient loading of large bitmaps, that will help you better in the treatment of these files.

Observing

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 question, it takes 1280 * 1024 * 4 = 5242880 B = 5242 KB = 5 MB.

Reply from the Soen

Browser other questions tagged

You are not signed in. Login or sign up in order to post.