Bitmap OOM

 老机器Bitmap预读仍然OOM,无奈增加一段,终于不崩溃了。

if (Build.VERSION.SDK_INT < 21)
    size = 2;

完整代码: 

Bitmap bitmap;
try {
    //Log.e(Thread.currentThread().getStackTrace()[2] + "", surl);
    URL url = new URL(surl);
    HttpURLConnection conn = (HttpURLConnection)url.openConnection();
    InputStream IS = conn.getInputStream();
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true; //开启只读尺寸
    bitmap = BitmapFactory.decodeStream(IS, null, options); //图片太大会崩溃
    WindowManager WM = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics DM = new DisplayMetrics();
    WM.getDefaultDisplay().getMetrics(DM);
    int size = 1;
    if (Build.VERSION.SDK_INT < 21)
        size = 2;
    else {
        if (options.outWidth > DM.widthPixels) {
            size = options.outWidth / DM.widthPixels;
        }
    }
    options.inSampleSize = size; //缩小比例
    Log.e(Thread.currentThread().getStackTrace()[2] + "", "if (" + options.outWidth + " > " + DM.widthPixels + ") inSampleSize = " + size);
    options.inJustDecodeBounds = false; //关闭只读尺寸
    conn = (HttpURLConnection)url.openConnection(); //InputStream使用后自动清空,需要重新获取
    IS = conn.getInputStream();
    bitmap = BitmapFactory.decodeStream(IS, null, options); //java.lang.OutOfMemoryError
    Message message = new Message();
    message.what = what;
    Bundle bundle = new Bundle();
    bundle.putString("url", surl);
    bundle.putParcelable("bitmap", bitmap);
    message.setData(bundle);
    handler.sendMessage(message);
} catch (Exception e) {
    Log.e(Thread.currentThread().getStackTrace()[2] + "", e.toString());
}

然而,媒体播放报错:Prepare failed,只好放弃了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值