Error placing image on Imagebutton

Asked

Viewed 143 times

1

When there was only one drawable folder this error was not happening, only that the app was distorted in other resolutions, so I created the drawable folders with different resolutions(ldpi, hdpi...) and inside them I put the image I want to set with its due DPI in each. From this the application began to break at the time the image should be set. If I delete the drawable folders and let only the default works.

04-26 14:42:13.261    1467-1467/com.app.gustavo.jogodavelha E/dalvikvm-heap﹕ 78925456-byte external allocation too large for this process.
04-26 14:42:13.280    1467-1469/com.app.gustavo.jogodavelha D/dalvikvm﹕ GC_FOR_MALLOC freed 4K, 50% free 2837K/5639K, external 2755K/3376K, paused 4ms
04-26 14:42:13.280    1467-1467/com.app.gustavo.jogodavelha E/GraphicsJNI﹕ VM won't let us allocate 78925456 bytes
04-26 14:42:13.280    1467-1467/com.app.gustavo.jogodavelha D/skia﹕ --- decoder->decode returned false
04-26 14:42:13.280    1467-1467/com.app.gustavo.jogodavelha D/AndroidRuntime﹕ Shutting down VM
04-26 14:42:13.280    1467-1467/com.app.gustavo.jogodavelha W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb3fe14f0)
04-26 14:42:13.280    1467-1467/com.app.gustavo.jogodavelha E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:2144)
            at android.view.View.performClick(View.java:2485)
            at android.view.View$PerformClick.run(View.java:9080)
            at android.os.Handler.handleCallback(Handler.java:587)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:130)
            at android.app.ActivityThread.main(ActivityThread.java:3683)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at android.view.View$1.onClick(View.java:2139)
            at android.view.View.performClick(View.java:2485)
            at android.view.View$PerformClick.run(View.java:9080)
            at android.os.Handler.handleCallback(Handler.java:587)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:130)
            at android.app.ActivityThread.main(ActivityThread.java:3683)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
            at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
            at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460)
            at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
            at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
            at android.content.res.Resources.loadDrawable(Resources.java:1709)
            at android.content.res.Resources.getDrawable(Resources.java:581)
            at android.view.View.setBackgroundResource(View.java:7533)
            at com.app.gustavo.jogodavelha.Main.clickQuadrado(Main.java:45)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at android.view.View$1.onClick(View.java:2139)
            at android.view.View.performClick(View.java:2485)
            at android.view.View$PerformClick.run(View.java:9080)
            at android.os.Handler.handleCallback(Handler.java:587)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:130)
            at android.app.ActivityThread.main(ActivityThread.java:3683)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
            at dalvik.system.NativeStart.main(Native Method)

  • Pole the log error and the code you are using to "set" the image.

  • I edited the question

  • This is the cause of the error: Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget. that in simple terms means that the image is too large, the memory is insufficient to treat this image.

  • Minutes, but why when I leave only one drawable folder does not appear error?

  • I think it must be because the image in that folder is smaller.

  • From what I understood from the error log, when loading the image there was a 78MB allocation. One suggestion I give is to implement a cache algorithm using the LRUCache and add the image at a reasonable resolution, something around 800x600 and let the ImageView scale using the scaleType or yourself while loading. Ah, another suggestion is to put the image in the folder drawable-nodpi.

  • That was really the problem thanks.

Show 2 more comments

2 answers

2

This error is a memory leak.

Your application is assigned a size of ~ 24MB. But the images you’re trying to set maybe, I say maybe they are, are the size of the stack.

This is happening because you are not freeing the memory, making the Android Dalvik VM does not take care of the GC-ing, native memory used by the app. When dealing with images on Android, you need to explicitly use reciclagem(). This releases the native memory.

For more deatils: Changing Imageview content causes Outofmemoryerror

But for the sake of conscience, try:

  1. Execute a clean
  2. A Rebuild
  3. Invalid Caches/Restart

1

The error happens due to poor memory of the emulator. You can add more memory. I advise using genymotion, which even in its free version is very useful for most applications.

Browser other questions tagged

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