部分華為設(shè)備上無(wú)法讀取bitmap的問(wèn)題解決方案

一商業(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;
}

完事。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容