欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

解決Android BitmapFactory的基本使用問題

 更新時(shí)間:2021年10月29日 11:09:16   作者:Just_Paranoid  
很多朋友給小編反饋使用方法BitmapFactory.decodeFile轉(zhuǎn)化Bitmap時(shí)報(bào)錯(cuò),究竟是什么原因?qū)е洛e(cuò)誤問題呢?今天通過本文給大家介紹下解決Android BitmapFactory的基本使用問題,感興趣的朋友一起看看吧

問題描述

使用方法BitmapFactory.decodeFile轉(zhuǎn)化Bitmap時(shí)報(bào)錯(cuò):java.lang.RuntimeException: Canvas: trying to draw too large(120422400bytes) bitmap.

解決方案

報(bào)錯(cuò)原因:圖片轉(zhuǎn)化為Bitmap超過最大值MAX_BITMAP_SIZE

frameworks/base/graphics/java/android/graphics/RecordingCanvas.java

public static final int MAX_BITMAP_SIZE = 100 * 1024 * 1024; // 100 MB

/** @hide */
@Override
protected void throwIfCannotDraw(Bitmap bitmap) {
    super.throwIfCannotDraw(bitmap);
    int bitmapSize = bitmap.getByteCount();
    if (bitmapSize > MAX_BITMAP_SIZE) {
        throw new RuntimeException(
                "Canvas: trying to draw too large(" + bitmapSize + "bytes) bitmap.");
    }
}

修改如下

//修改前
Bitmap image = BitmapFactory.decodeFile(filePath);
imageView.setImageBitmap(image);

//修改后
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.DisplayMetrics;

File file = new File(filePath);
if (file.exists() && file.length() > 0) {
    //獲取設(shè)備屏幕大小
    DisplayMetrics dm = getResources().getDisplayMetrics();
    int screenWidth = dm.widthPixels;
    int screenHeight = dm.heightPixels;
    //獲取圖片寬高
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;	
    BitmapFactory.decodeFile(filePath, options);	
    float srcWidth = options.outWidth;	
    float srcHeight = options.outHeight;	
    //計(jì)算縮放比例
    int inSampleSize = 1;	
    if (srcHeight > screenHeight || srcWidth > screenWidth) {	
        if (srcWidth > srcHeight) {	
            inSampleSize = Math.round(srcHeight / screenHeight);	
        } else {	
            inSampleSize = Math.round(srcWidth / screenWidth);	
        }	
    }	
    options.inJustDecodeBounds = false;	
    options.inSampleSize = inSampleSize;	
    
    Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);	
    imageView.setImageBitmap(bitmap);
}

相關(guān)參考:

Android 圖片緩存之 Bitmap 詳解
https://juejin.cn/post/6844903442939412493

BitmapFactory
https://developer.android.com/reference/android/graphics/BitmapFactory.html

BitmapFactory.options
BitmapFactory.Options類是BitmapFactory對圖片進(jìn)行解碼時(shí)使用的一個(gè)配置參數(shù)類,其中定義了一系列的public成員變量,每個(gè)成員變量代表一個(gè)配置參數(shù)。
https://blog.csdn.net/showdy/article/details/54378637

到此這篇關(guān)于Android BitmapFactory的基本使用的文章就介紹到這了,更多相關(guān)Android BitmapFactory使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論