解決Android BitmapFactory的基本使用問(wèn)題
問(wèn)題描述
使用方法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超過(guò)最大值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對(duì)圖片進(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Templates實(shí)戰(zhàn)之更優(yōu)雅實(shí)現(xiàn)自定義View構(gòu)造方法詳解
本篇文章介紹如何利用Android Studio提供的Live Templates更優(yōu)雅實(shí)現(xiàn)自定義View的構(gòu)造方法,說(shuō)句人話就是:簡(jiǎn)化自定義View構(gòu)造參數(shù)模板代碼的編寫,實(shí)現(xiàn)自動(dòng)生成,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Android登錄記住多個(gè)密碼的實(shí)現(xiàn)方法
通過(guò)在popouWindow里面加上ListView,數(shù)據(jù)是把List以字符串按照J(rèn)SON的樣式存入本地,下面通過(guò)實(shí)例代碼給大家介紹android登錄記住多個(gè)密碼的實(shí)現(xiàn)方法,需要的的朋友參考下吧2017-07-07
Android Studio無(wú)法改變Button背景顏色解決辦法
今天我來(lái)和大家探討一個(gè)在Android開(kāi)發(fā)中常見(jiàn)但可能讓初學(xué)者感到困惑的問(wèn)題,如何在Android Studio中改變Button的背景顏色,這個(gè)問(wèn)題看似簡(jiǎn)單,但實(shí)際操作中可能會(huì)遇到一些意想不到的挑戰(zhàn),接下來(lái),我將從多個(gè)角度為大家提供解決方案,需要的朋友可以參考下2024-05-05
Android UI設(shè)計(jì)系列之自定義Dialog實(shí)現(xiàn)各種風(fēng)格的對(duì)話框效果(7)
這篇文章主要介紹了Android UI設(shè)計(jì)系列之自定義Dialog實(shí)現(xiàn)各種風(fēng)格的對(duì)話框效果,具有一定的實(shí)用性和參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06
Android CardView+ViewPager實(shí)現(xiàn)ViewPager翻頁(yè)動(dòng)畫(huà)的方法
本篇文章主要介紹了Android CardView+ViewPager實(shí)現(xiàn)ViewPager翻頁(yè)動(dòng)畫(huà)的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-06-06
Android Socket實(shí)現(xiàn)多個(gè)客戶端即時(shí)通信聊天
這篇文章主要為大家詳細(xì)介紹了Android Socket實(shí)現(xiàn)多個(gè)客戶端即時(shí)通信聊天,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
完美解決安卓jni項(xiàng)目會(huì)刪除其他so文件的問(wèn)題
下面小編就為大家?guī)?lái)一篇完美解決安卓jni項(xiàng)目會(huì)刪除其他so文件的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12
Android開(kāi)發(fā)之Android.mk模板的實(shí)例詳解
這篇文章主要介紹了Android開(kāi)發(fā)之Android.mk模板的實(shí)例詳解的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下2017-10-10

