Android中加载多个大型图OutOfMemoryError的问题解决。



手持机项目里集体卡PagerAdapter显示滑动多张人员头像照片,滑动预览时崩溃。OOM内存溢出。转载帖子:很多照片是好几MB一张,因为目前的Android系统对运行的程序都有一定的内存限制,一般是16MB或24MB(视平台而定),不做处理直接加载的话必然会报OOM (Out Of Memmory)。网上有很多解决android加载bitmap内存溢出的方法,下面是我转载出来的代码:

**
 *
 * Solve the problem of OutOfMemoryError ,by loading multiple large graphs(large Bitmap) in Android.
 * Bitmap bitmap = BitmapFactory.decodeByteArray(Util.decodeBitmap(imagePath), 0, Util.decodeBitmap(imagePath).length);
 */

public static byte[] decodeBitmap(String path) {
    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inJustDecodeBounds = true;// 设置成了true,不占用内存,只获取bitmap宽高
    BitmapFactory.decodeFile(path, opts);
    opts.inSampleSize = computeSampleSize(opts, -1, 1024 * 800);
    opts.inJustDecodeBounds = false;// 这里一定要将其设置回false,因为之前我们将其设置成了true
    opts.inPurgeable = true;
    opts.inInputShareable = true;
    opts.inDither = false;
    opts.inPurgeable = true;
    opts.inTempStorage = new byte[16 * 1024];
    FileInputStream is = null;
    Bitmap bmp = null;
    ByteArrayOutputStream baos = null;
    try {
        is = new FileInputStream(path);
        bmp = BitmapFactory.decodeFileDescriptor(is.getFD(), null, opts);
        double scale = getScaling(opts.outWidth * opts.outHeight, 1024 * 600);
        Bitmap bmp2 = Bitmap.createScaledBitmap(bmp,
                (int) (opts.outWidth * scale),
                (int) (opts.outHeight * scale), true);
        bmp.recycle();
        baos = new ByteArrayOutputStream();
        bmp2.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        bmp2.recycle();
        return baos.toByteArray();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
            baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.gc();
    }
    return baos.toByteArray();
}

    private static double getScaling(int src, int des) {
        /**
         * 48 目标尺寸÷原尺寸 sqrt开方,得出宽高百分比 49
         */
        double scale = Math.sqrt((double) des / (double) src);
        return scale;
    }
    public static int computeSampleSize(BitmapFactory.Options options,
                                        int minSideLength, int maxNumOfPixels) {
        int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels);
        int roundedSize;
        if (initialSize <= 8) {
            roundedSize = 1;
            while (roundedSize < initialSize) {
                roundedSize <<= 1;
            }
        } else {
            roundedSize = (initialSize + 7) / 8 * 8;
        }
        return roundedSize;
    }
    private static int computeInitialSampleSize(BitmapFactory.Options options,
                                                int minSideLength, int maxNumOfPixels) {
        double w = options.outWidth;
        double h = options.outHeight;

        int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math
                .sqrt(w * h / maxNumOfPixels));
        int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(
                Math.floor(w / minSideLength), Math.floor(h / minSideLength));

        if (upperBound < lowerBound) {
            return lowerBound;
        }

        if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
            return 1;
        } else if (minSideLength == -1) {
            return lowerBound;
        } else {
            return upperBound;
        }
    }
阅读更多 登录后自动展开
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页