解決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)文章
android tv列表焦點(diǎn)記憶實(shí)現(xiàn)的方法
本篇文章主要介紹了android tv列表焦點(diǎn)記憶實(shí)現(xiàn)的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04仿網(wǎng)易新聞客戶端頭條ViewPager嵌套實(shí)例
正確使用requestDisallowInterceptTouchEvent(boolean flag)方法,下面為大家介紹下外層ViewPager布局的實(shí)例,感興趣的朋友可以參考下哈2013-06-06Android自定義滑動(dòng)刪除效果的實(shí)現(xiàn)代碼
這篇文章將從現(xiàn)有 Android 滑動(dòng)刪除的痛點(diǎn),到搭建好一個(gè)基本的框架,到最終提供一份完整的 Demo為止,爭取為讀者提供最大的可定制化,需要的朋友可以參考下2018-03-03Android編程實(shí)現(xiàn)監(jiān)聽EditText變化的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)監(jiān)聽EditText變化的方法,涉及Android針對EditText的相關(guān)操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11Android進(jìn)階之使用時(shí)間戳計(jì)算時(shí)間差
這篇文章主要為大家詳細(xì)介紹了Android進(jìn)階之使用時(shí)間戳計(jì)算時(shí)間差,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Android 中Crash時(shí)如何獲取異常信息詳解及實(shí)例
這篇文章主要介紹了Android 中Crash時(shí)如何獲取異常信息詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-02-02Android實(shí)現(xiàn)有道辭典查詢功能實(shí)例詳解
這篇文章主要介紹了Android實(shí)現(xiàn)有道辭典查詢功能的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android基于有道詞典查詢功能的原理與具體實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-10-10