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

解決Android BitmapFactory的基本使用問題

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

問題描述

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

解決方案

報錯原因:圖片轉(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;	
    //計算縮放比例
    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ù)類,其中定義了一系列的public成員變量,每個成員變量代表一個配置參數(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列表焦點記憶實現(xiàn)的方法

    android tv列表焦點記憶實現(xiàn)的方法

    本篇文章主要介紹了android tv列表焦點記憶實現(xiàn)的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04
  • Android自定義定時鬧鐘開發(fā)

    Android自定義定時鬧鐘開發(fā)

    這篇文章主要為大家詳細(xì)介紹了Android自定義定時鬧鐘開發(fā),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-09-09
  • 仿網(wǎng)易新聞客戶端頭條ViewPager嵌套實例

    仿網(wǎng)易新聞客戶端頭條ViewPager嵌套實例

    正確使用requestDisallowInterceptTouchEvent(boolean flag)方法,下面為大家介紹下外層ViewPager布局的實例,感興趣的朋友可以參考下哈
    2013-06-06
  • Android自定義滑動刪除效果的實現(xiàn)代碼

    Android自定義滑動刪除效果的實現(xiàn)代碼

    這篇文章將從現(xiàn)有 Android 滑動刪除的痛點,到搭建好一個基本的框架,到最終提供一份完整的 Demo為止,爭取為讀者提供最大的可定制化,需要的朋友可以參考下
    2018-03-03
  • Android編程實現(xiàn)監(jiān)聽EditText變化的方法

    Android編程實現(xiàn)監(jiān)聽EditText變化的方法

    這篇文章主要介紹了Android編程實現(xiàn)監(jiān)聽EditText變化的方法,涉及Android針對EditText的相關(guān)操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-11-11
  • Android進(jìn)階之使用時間戳計算時間差

    Android進(jìn)階之使用時間戳計算時間差

    這篇文章主要為大家詳細(xì)介紹了Android進(jìn)階之使用時間戳計算時間差,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Android 中Crash時如何獲取異常信息詳解及實例

    Android 中Crash時如何獲取異常信息詳解及實例

    這篇文章主要介紹了Android 中Crash時如何獲取異常信息詳解及實例的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • Android實現(xiàn)有道辭典查詢功能實例詳解

    Android實現(xiàn)有道辭典查詢功能實例詳解

    這篇文章主要介紹了Android實現(xiàn)有道辭典查詢功能的方法,結(jié)合實例形式較為詳細(xì)的分析了Android基于有道詞典查詢功能的原理與具體實現(xiàn)技巧,需要的朋友可以參考下
    2016-10-10
  • 如何在Android App中集成支付寶和微信支付功能

    如何在Android App中集成支付寶和微信支付功能

    支付是各位Android開發(fā)者們在日常工作中經(jīng)常會遇到的一個需求,下面這篇文章主要給大家介紹了關(guān)于如何在Android App中集成支付寶和微信支付功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-05-05
  • 詳解android 通過uri獲取bitmap圖片并壓縮

    詳解android 通過uri獲取bitmap圖片并壓縮

    這篇文章主要介紹了詳解android 通過uri獲取bitmap圖片并壓縮的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解這部分內(nèi)容,需要的朋友可以參考下
    2017-10-10

最新評論