Android 中 加载Bitmap时,造成的Out of memory 问题

在Android中,对图片使用的内存是有限制的,加载的图片过大便出导致OOM问题。图像在加载过程中,是把所有像素(即长*宽)加载到内存中,如果图片过大,便会导致java.lang.OutOfMemoryError问题,因此,在使用时要要加以注意。



 private static int MAX_IMAGE_DIMENSION = 720;

    public Bitmap decodeFile(String filePath) throws IOException {

        Uri photoUri = Uri.parse(filePath);
        InputStream is = this.getContentResolver().openInputStream(photoUri);
        BitmapFactory.Options dbo = new BitmapFactory.Options();
        dbo.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(is, null, dbo);
        is.close();

        int rotatedWidth, rotatedHeight;
 	rotatedWidth = dbo.outWidth;
        rotatedHeight = dbo.outHeight;
       

        Bitmap mCurrentBitmap = null;
        is = this.getContentResolver().openInputStream(photoUri);
        if (rotatedWidth > MAX_IMAGE_DIMENSION || rotatedHeight > MAX_IMAGE_DIMENSION) {
            float widthRatio = ((float) rotatedWidth) / MAX_IMAGE_DIMENSION;
            float heightRatio = ((float) rotatedHeight) / MAX_IMAGE_DIMENSION;
            float maxRatio = Math.max(widthRatio, heightRatio);
            // Create the bitmap from file
            BitmapFactory.Options options = new BitmapFactory.Options();
            // 1.换算合适的图片缩放值,以减少对JVM太多的内存请求。
            options.inSampleSize = (int) maxRatio;
            // 2. inPurgeable 设定为 true,可以让java系统, 在内存不足时先行回收部分的内存
            options.inPurgeable = true;
            // 与inPurgeable 一起使用
            options.inInputShareable = true;
            // 3. 减少对Aphla 通道
            options.inPreferredConfig = Bitmap.Config.RGB_565;
            try {
                // 4. inNativeAlloc 属性设置为true,可以不把使用的内存算到VM里
                BitmapFactory.Options.class.getField("inNativeAlloc").setBoolean(options, true);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (SecurityException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
            // 5. 使用decodeStream 解码,则利用NDK层中,利用nativeDecodeAsset()
            // 进行解码,不用CreateBitmap
            mCurrentBitmap = BitmapFactory.decodeStream(is, null, options);
        } else {
            mCurrentBitmap = BitmapFactory.decodeStream(is);
        }
        return mCurrentBitmap;
    }


评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值