简体   繁体   中英

Is there a limit on the size of image drawable in android?

I am trying to load a 3MB image using getDrawable() for testing purposes, but I get an out of memory leak exception. Is there any limit on the size of image drawable you can use in android ?

java.lang.OutOfMemoryError
            at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
            at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:594)
            at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:429)
            at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
            at android.content.res.Resources.loadDrawable(Resources.java:2113)
            at android.content.res.Resources.getDrawable(Resources.java:700)
            at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:321)
            at com.testleak.MainActivity.onCreate(MainActivity.java:22)
            at android.app.Activity.performCreate(Activity.java:5248)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
            at android.app.ActivityThread.access$800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5097)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

Is there any limit on the size of image drawable you can use in android ?

Yes. It has to fit in available memory when decoded . A 3MB PNG or JPEG file will be much larger when decoded, as the decoded memory usage is width x height x 4 bytes/pixel.

"Available memory" depends on:

  • The heap limit imposed by your device, based on installed system RAM (see ActivityManager and getMemoryClass() )

  • The largest single block of free memory within your heap, which will depend on a lot of factors at runtime

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM