一商业项目后台崩溃信息上报发现在华为设备上读取图片闪退,经过检查应该是内存溢出,解决方案为在读取图片时进行适当压缩以节省内存占用,目前已在荣耀畅想 9 Plus测试通过,方法如下:
public static Bitmap getBitmapFromUri(Context context, Uri uri) {
try {
Bitmap bitmap = getBitmap(context.getContentResolver(), uri);
return bitmap;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static final Bitmap getBitmap(ContentResolver cr, Uri url) throws FileNotFoundException, IOException {
InputStream input = cr.openInputStream(url);
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inTempStorage = new byte[100 * 1024];
opts.inPurgeable = true;
String factory = Build.BRAND.toUpperCase();
if (factory.contains("HUAWEI")||factory.contains("HONOR")) {
opts.inSampleSize = 2;
}
opts.inInputShareable = true;
opts.inPreferredConfig = Bitmap.Config.RGB_565;
Bitmap bitmap = BitmapFactory.decodeStream(input, null, opts);
input.close();
return bitmap;
}
完事。
网友评论