Android BitmapFactory.decodeResource占用过多内存

Jaskmar

我对从资源加载位图有疑问。我的代码:

public void onClick(View view) {
    if (mainButton == view) {
        Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.test);
    }
}

test.jpg图像分辨率为3288 x 4936px。它是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 for Android中使用资源中的相同图像编写了类似的应用程序,并且工作正常。

编辑:

  1. 我无法调整大小。

  2. 这是非常简单的应用程序,例如hello world。它什么也没做,只是从资源中加载位图。

编辑2:

吉姆的解决方案对我有用。但是还有另一个问题。位图加载后,它太大了4倍(高度的2倍和宽度的2倍)。我尝试了各种图像,包括在Pinta或Gimp中创建的新图像。

Jaskmar

好的,我分两个步骤解决了我的问题:

1)根据吉姆的回答,我添加了

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);

感谢您的所有答复。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如果多次使用,则Android BitmapFactory.decodeResource内存不足

Backstack上的Android碎片占用过多内存

BitmapFactory.decodeResource在Android 2.2上为null

为什么Android BitmapFactory.decodeFile()需要4倍的内存?

当BitmapFactory.nativeDecodeStream()耗尽android应用程序的内存时

Android BitmapFactory与BufferedInputStream

BitmapFactory.decodeResource在Android 2.2中返回可变的位图,在Android 1.6中返回不可变的位图

Android:BitmapFactory解码速度缓慢

gvfsd占用过多内存

Firefox占用过多内存

projectRaster占用过多内存

通过bitmapfactory.decoderesource加载图像

Android约束布局使用过多的内存(RAM)(内存泄漏)

如何使用 Android BitmapFactory.Options.inBitmap?

HashSet of Strings占用过多内存,建议...?

Python / lxml占用过多内存

Elixir进程占用过多内存

占用过多内存的对象的Python列表

WPF ListboxItems占用过多内存

Android 5.0不支持BitmapFactory.Options inPurgeable

来自Assets的BitmapFactory.decodeStream在Android 7上返回null

BitmapFactory.decodeFile() 返回 null xamarin.android

Android BitmapFactory.decodeFile(path)始终返回null

Android Studio Kotlin:BitmapFactory.decodeStream()返回null

Android中的BitmapFactory.decodeFile和ImageView奇怪的东西

在android.graphics.BitmapFactory.nativeDecodeAsset(本地方法)

Grails应用程序占用过多内存

在iOS中加载GIF会占用过多内存

我的SDL / C ++程序是否占用过多的RAM内存?