显示高分辨率图像内存错误

时间:2016-09-20 01:15:53

标签: android memory-management out-of-memory bitmapfactory

我尝试在启动画面活动中显示1080x600 png,并在BitmapFactory.nativeDecodeAsset(decodeStream)中获得和OutOfMemoryError。

所以我决定在使用这种方法解码资源之前计算InSampleSize:

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

这适用于480x800,720x1280,1080x1920没有错误,但是当我在我的朋友nexus 5(1080x1920)中打开它时它崩溃了,因为它加载了真实尺寸的png。

所以现在我很困惑,无法在不破坏高分辨率手机图片质量的情况下想出更好的方法。

0 个答案:

没有答案