Error while placing image in ImageButton

1

When there was only a drawable folder this error was not happening, but the app was distorted in other resolutions, hence I created the drawable folders with different resolutions (ldpi, hdpi ...) and inside I put the image that I want to set with its due DPI in each. From this the application started to break at the time the image should be set. If I delete the drawable folders and leave 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)
    
asked by anonymous 26.04.2015 / 16:10

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 up maybe, I say maybe they are, whatever the size of the stack.

This is happening because you are not releasing memory, causing the Android Dalvik VM to not take care of the% -co_de% -ing, native memory used by the application. When dealing with images on Android, you must explicitly use GC . This frees up native memory.

For more deatils: Changing ImageView content causes OutOfMemoryError

But for disenchantment, try:

  • Run reciclagem()
  • A% of%
  • clean
  • 25.08.2016 / 14:29
    1

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

        
    26.04.2015 / 21:08