分享

Android Out of memory (many bitmaps)

 linf_11 2015-07-23

Its a known bug, its not because of large files.

Bitmap is stored in native heap, but it will get garbage collected automatically, calling recycle() or by other process by making weakreference or softreference To fix OutOfMemory you should do something like this:

options.inSampleSize = calculateInSampleSize(options,bitmapWidth,bitmapHeight);

public static int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {

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

For example, an image with resolution 2048x1536 that is decoded with an inSampleSize of 4 produces a bitmap of approximately 512x384

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多