图片 java.lang.OutOfMemoryError

BitmapFactory.Options factory = new BitmapFactory.Options();
factory = AndroidUtil.setOptions(factory);
    ByteBuffer byteBuffer = AndroidUtil.loadImage(appfilepath + "/" + path + "/"
+ card.getCardId() + "/" + Values.USER_UUID+ "/PHOTO/PHOTO.jpg");
byte[] byteData = byteBuffer.array();
Bitmap bm = BitmapFactory.decodeByteArray(byteData, 0, byteData.length, factory);
 BitmapDrawable bd = new BitmapDrawable(bm);
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {login_icon.setBackgroundDrawable(bd); }
 else
  {login_icon.setBackground(bd); }

//===========================================================================// 
public static BitmapFactory.Options setOptions(BitmapFactory.Options opts) {
    opts.inJustDecodeBounds = false;
    opts.inPurgeable = true;
    opts.inInputShareable = true;
    opts.inPreferredConfig = Bitmap.Config.RGB_565;
    opts.inSampleSize = 1;
    return opts;
}

        //====================================================================//
          public static ByteBuffer loadImage(String imagePath) throws IOException, OutOfMemoryError {
    FileInputStream fis = null;
    ByteBuffer byteBuffer = null;
    try {
        File file = new File(imagePath);
        fis = new FileInputStream(file);
        int length = fis.available();
        byteBuffer = ByteBuffer.allocate(length);
        if (length != -1) {
            byte[] temp = new byte[512];
            int readLen = 0;
            while ((readLen = fis.read(temp)) > 0) {
                byteBuffer.put(temp, 0, readLen);
            }
        }
        return byteBuffer;
    }
    catch(OutOfMemoryError e) {
        System.gc();
        e.printStackTrace();
        byteBuffer = null;
        throw e;
    }
    catch(IOException e) {
        e.printStackTrace();
        byteBuffer = null;
        return null;
    } finally {
        if (fis != null) {
            fis.close();
            fis = null;
        }
    }
}

//====================导致内存溢出的代码============================//
BitmapFactory FileInputStream fis = new FileInputStream(userLogo);
 Drawable drawable = new BitmapDrawable(null,BitmapFactory.decodeStream(fis));
            login_icon.setImageDrawable(drawable);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值