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