老机器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,只好放弃了。




![【饿了么笔试题汇总】[全网首发]2024-04-12-饿了么春招笔试题-三语言题解(CPP/Python/Java)](https://img-blog.csdnimg.cn/direct/60dd47443c3048618c27e2778460ffae.png#pic_center)

![[lesson20]初始化列表的使用](https://img-blog.csdnimg.cn/direct/aab952e1761943ceb67416ac8b644ff4.png#pic_center)




![[RK3399 Linux] 使用ubuntu 20.04.5制作rootfs](https://img-blog.csdnimg.cn/direct/ac972cc3684b445abe2d6d0d2a62d116.png#pic_center)








