一商業(yè)項(xiàng)目后臺(tái)崩潰信息上報(bào)發(fā)現(xiàn)在華為設(shè)備上讀取圖片閃退,經(jīng)過(guò)檢查應(yīng)該是內(nèi)存溢出,解決方案為在讀取圖片時(shí)進(jìn)行適當(dāng)壓縮以節(jié)省內(nèi)存占用,目前已在榮耀暢想 9 Plus測(cè)試通過(guò),方法如下:
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;
}
完事。