简体   繁体   English

android中可绘制图像的大小是否有限制?

[英]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. 我正在尝试使用getDrawable()加载3MB图像以进行测试,但出现内存不足泄漏异常。 Is there any limit on the size of image drawable you can use in android ? 您可以在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 ? 您可以在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. 解码后的3MB PNG或JPEG文件将大得多,因为解码的内存使用量是宽度x高度x 4字节/像素。

"Available memory" depends on: “可用内存”取决于:

  • The heap limit imposed by your device, based on installed system RAM (see ActivityManager and getMemoryClass() ) 设备根据已安装的系统RAM施加的堆限制(请参阅ActivityManagergetMemoryClass()

  • The largest single block of free memory within your heap, which will depend on a lot of factors at runtime 堆中最大的空闲内存块,这取决于运行时的许多因素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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