每次尝试显示背景图像时,每次使用Galaxy S5时我都会遇到崩溃。

此背景位于xxhdpi资源文件夹中,其大小与S5屏幕(1080x1920)相同,因此我无需调用“createScaledBitmap”即可对其进行缩放。该图像的分辨率是JPG 96dpi。

当调用解码资源...崩溃!这怎么可能?是我在此“超强大”设备中加载的唯一位图。

谢谢!!!

在我的代码下方(S5的比例= 1):

public static Bitmap decodeBitmapFromResource(Resources res, int resId, float scale) {

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    options.inSampleSize = calculateInSampleSize(options,
            (int)(options.outWidth*scale),
            (int)(options.outHeight*scale));

    options.inJustDecodeBounds = false;

    if (scale > 1) {
        Bitmap bitmap = BitmapFactory.decodeResource(res, resId);
        return Bitmap.createScaledBitmap(bitmap, (int)(options.outWidth*scale),
                (int)(options.outHeight*scale), true);
    }

    return BitmapFactory.decodeResource(res, resId, options);
}

最佳答案

在您的清单文件中的application标签中添加此行。它不能解决问题,只是让您的应用拥有更多内存:

 android:largeHeap="true"

更新:

但是,使用largeHeap并不是一个好的解决方案。这是谷歌关于此的文档。



关于加载位图:



看一下该页面也不错,它说明了管理内存的方法:

How Your App Should Manage Memory

因此,我认为我的最后答案不是一个好的解决方案,您可能会重新考虑加载图像的策略。希望这个答案对您有帮助;)

07-24 20:25