Android BitmapFactory.decodeResource 占用太多内存
Android BitmapFactory.decodeResource takes too much memory
我对从资源加载位图有疑问。我的代码:
public void onClick(View view) {
if (mainButton == view) {
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.test);
}
}
test.jpg 图片分辨率为 3288 x 4936 像素。它是 jpeg(未压缩时为 3.9MB / 48.7MB)。虽然此功能有效(在我的 Nexus 7 2013 设备上),但出现以下异常:
java.lang.OutOfMemoryError: Failed to allocate a 259673100 byte allocation with 5222644 free bytes and 184MB 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.BitmapFactory.decodeResource(BitmapFactory.java:467)
at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:497)
at pl.jaskol.androidtest.MainActivity.onClick(MainActivity.java:50)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
为什么应用程序尝试分配多达 248MB 的空间?
我在 Qt 中为 Android 编写了类似的应用程序,在资源中使用了相同的图像,并且工作正常。
编辑:
我无法调整大小。
这是一个非常简单的应用程序,就像你好世界。它什么都不做,只是从资源中加载位图。
编辑2:
Jim 的解决方案对我有用。但是还有一个问题。加载位图后,它太大了 4 倍(2 倍高和 2 倍宽)。我尝试了各种图像,包括在 Pinta 或 Gimp 中创建的新图像。
看看这个资源:http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
特别是decodeSampledBitmapFromResource
方法。
您可以在 AndroidManifest.xml
文件中的应用程序标签上使用 android:largeHeap="true"
来超过 64MB
限制。这不适用于 pre 3.0
设备。
这可能会立即解决您的问题。如果这还不够,您可以使用本机代码加载位图,其中您的堆限制是设备上的全部内存。 NDK
涉及更多,因此请先尝试上述解决方案。
好的,我分两步解决了我的问题:
1) 根据 Jim 的回答我添加了
android:largeHeap="true"
在 AndroidManifest.xml
文件中我的应用程序标签上。
2) 由于我设备上的 DPI
高屏幕,图像已自动调整大小。为了避免这种情况,我必须将 inScaled 选项设置为 false:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.raw.test, options);
感谢您的所有回答。
我对从资源加载位图有疑问。我的代码:
public void onClick(View view) {
if (mainButton == view) {
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.test);
}
}
test.jpg 图片分辨率为 3288 x 4936 像素。它是 jpeg(未压缩时为 3.9MB / 48.7MB)。虽然此功能有效(在我的 Nexus 7 2013 设备上),但出现以下异常:
java.lang.OutOfMemoryError: Failed to allocate a 259673100 byte allocation with 5222644 free bytes and 184MB 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.BitmapFactory.decodeResource(BitmapFactory.java:467)
at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:497)
at pl.jaskol.androidtest.MainActivity.onClick(MainActivity.java:50)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
为什么应用程序尝试分配多达 248MB 的空间? 我在 Qt 中为 Android 编写了类似的应用程序,在资源中使用了相同的图像,并且工作正常。
编辑:
我无法调整大小。
这是一个非常简单的应用程序,就像你好世界。它什么都不做,只是从资源中加载位图。
编辑2:
Jim 的解决方案对我有用。但是还有一个问题。加载位图后,它太大了 4 倍(2 倍高和 2 倍宽)。我尝试了各种图像,包括在 Pinta 或 Gimp 中创建的新图像。
看看这个资源:http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
特别是decodeSampledBitmapFromResource
方法。
您可以在 AndroidManifest.xml
文件中的应用程序标签上使用 android:largeHeap="true"
来超过 64MB
限制。这不适用于 pre 3.0
设备。
这可能会立即解决您的问题。如果这还不够,您可以使用本机代码加载位图,其中您的堆限制是设备上的全部内存。 NDK
涉及更多,因此请先尝试上述解决方案。
好的,我分两步解决了我的问题:
1) 根据 Jim 的回答我添加了
android:largeHeap="true"
在 AndroidManifest.xml
文件中我的应用程序标签上。
2) 由于我设备上的 DPI
高屏幕,图像已自动调整大小。为了避免这种情况,我必须将 inScaled 选项设置为 false:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.raw.test, options);
感谢您的所有回答。