即使在使用高效加载大位图指南后如何避免 OutOfMemoryError

How to avoid OutOfMemoryError even after having used the Loading Large Bitmap Efficiently guidelines

我正在尝试设置 setImageBitmap,但即使在我实施了此处的指南后,我仍然收到以下 OutOfMemoryError https://developer.android.com/topic/performance/graphics/load-bitmap.html:

java.lang.OutOfMemoryError: Failed to allocate a 11345668 byte allocation with 1206384 free bytes and 1178KB until OOM
        at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
        at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
        at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:620)
        at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:455)
        at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:478)
        at com.example.myapp.ImageOptimized.decodeSampledBitmapFromResource(ImagenOptimizada.java:50)
        at com.example.myapp.Game3Players.onAnimationEnd(Game3Players.java:511)
        at android.view.animation.Animation.run(Animation.java:381)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

这是我使用该方法的代码部分。我随机选择一张卡片显示一张图片,其余显示另一张:

ImageView[] cardsArray = new ImageView[3];
            cardsArray[0]=cardOne;
            cardsArray[1]=cardTwo;
            cardsArray[2]=cardThree;

            final int index = new Random().nextInt(cardsArray.length);                                
            cardsArray[index].setImageBitmap(ImageOptimized.decodeSampledBitmapFromResource(getResources(), R.drawable.skull, 250, 250));     

            for (int i=0; i<cardsArray.length; i++){                                                  

                if (cardsArray[index]!=cardsArray[i]){
                    cardsArray[i].setImageBitmap(ImageOptimized.decodeSampledBitmapFromResource(getResources(), R.drawable.safe, 250, 250));
                }

            }

我第一次尝试这个是因为,因为我要用一张图片替换另一张图片,所以我想使用相同的宽度和高度:

cardsArray[index].setImageBitmap(ImageOptimized.decodeSampledBitmapFromResource(getResources(), R.drawable.skull, cardsArray[index].getWidth(), cardsArray[index].getHeight()));

但这立即给我一个错误。这就是我决定使用固定大小 250 的原因,但它一直向我抛出错误。有趣的是,有时代码运行没有问题,但当重复操作时——可能是第三次、第四次,有时是第五次——它就会崩溃。我可能做错了什么导致内存泄漏吗?

以防万一你也想看看 class 我遵循的指南:

public class ImageOptimized {


    public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) >= reqHeight
                    && (halfWidth / inSampleSize) >= reqWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
    }


    public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
                                                         int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res, resId, options);
    }

}

我几天前遇到了同样的问题,我错过了我的 drawable 的 copy/paste 而不是把它放在 drawable-hdpi (例如)我把它放在 drawable 文件夹,没有扩展名,将可绘制对象移动到带扩展名的可绘制文件夹对我有用。

您也可以将这些行添加到您的 manifest.xml

android:hardwareAccelerated="false"

android:largeHeap="true"

您可以使用加载和缓存图像库以获得更好的性能,例如 Glide or Picasso

例如 Glide 的使用非常简单:

Glide.with(this).load(R.drawable.safe).into(imageView);

您也可以调整它们的大小:

Glide.with(this).load(R.drawable.safe).apply(new RequestOptions().override(250, 250).into(imageView);

这些库中的大多数都自行处理所用资源的清理工作;这意味着它们可以有效地处理内存。

另外尽量不要使用非常大的分辨率图片,如果可以选择使用PNG格式。