android通过BitmapFactory.decodeFile获取图片bitmap报内存溢出的解决办法

时间:2022-02-08 21:38:59

  android通过BitmapFactory.decodeFile获取图片bitmap报内存溢出的解决办法

  原方法:

    public static Bitmap getSmallBitmap(String filePath, int reqWidth, int reqHeight) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options); // Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(filePath, options);
}

  异常:

06-23 11:41:04.817 24959-24959/com.test.tax E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.test.tax, PID: 24959
java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:623)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:599)
at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:378)
at com.test.tax.utils.PictureUtils.getSmallBitmap(PictureUtils.java:138)
at com.test.tax.utils.PictureUtils.bitmapToFile(PictureUtils.java:58)
at com.test.tax.utils.localalbum.ui.LocalAlbum.onActivityResult(LocalAlbum.java:141)
at android.app.Activity.dispatchActivityResult(Activity.java:5441)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3353)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3400)
at android.app.ActivityThread.access$1300(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1250)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5047)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)

  解决办法:

  通过设置BitmapFactory.Options属性解决

        options.inPreferredConfig = Bitmap.Config.RGB_565;
options.inDither = true;

  解决后的方法:

    public static Bitmap getSmallBitmap(String filePath, int reqWidth, int reqHeight) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options); // Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
//避免出现内存溢出的情况,进行相应的属性设置。
options.inPreferredConfig = Bitmap.Config.RGB_565;
options.inDither = true; return BitmapFactory.decodeFile(filePath, options);
}

android通过BitmapFactory.decodeFile获取图片bitmap报内存溢出的解决办法的更多相关文章

  1. WPF循环加载图片导致内存溢出的解决办法

    程序场景:一系列的图片,从第一张到最后一张依次加载图片,形成“动画”. 生成BitmapImage的方法有多种: 1. var source=new BitmapImage(new Uri(&quot ...

  2. ANGULAR 使用 ng build --prod 编译报内存错误的解决办法

    如果你遇到如下的情况 <--- Last few GCs ---> [13724:0000020D39C660D0] 231298 ms: Mark-sweep 1356.3 (1433. ...

  3. Android相机、相册获取图片显示并保存到SD卡

    Android相机.相册获取图片显示并保存到SD卡 [复制链接]   电梯直达 楼主    发表于 2013-3-13 19:51:43 | 只看该作者 |只看大图  本帖最后由 happy小妖同学 ...

  4. Android性能优化之利用LeakCanary检测内存泄漏及解决办法

    前言: 最近公司C轮融资成功了,移动团队准备扩大一下,需要招聘Android开发工程师,陆陆续续面试了几位Android应聘者,面试过程中聊到性能优化中如何避免内存泄漏问题时,很少有人全面的回答上来. ...

  5. Android开发常见的Activity中内存泄漏及解决办法

    上一篇文章楼主提到由Context引发的内存泄漏,在这一篇文章里,我们来谈谈Android开发中常见的Activity内存泄漏及解决办法.本文将会以“为什么”“怎么解决”的方式来介绍这几种内存泄漏. ...

  6. UiAutomatorViewer无法获取手机截图进行元素定位的解决办法

    问题描述 本来想使用UIAutomatorView定位app页面元素的,最开始我使用的是夜神模拟器,打开UIAutomatorView连接模拟器没有问题,但是后来我使用真机时发现无法连接到真机获取真机 ...

  7. &period;NET客户端下载SQL Server数据库中文件流保存的大电子文件方法(不会报内存溢出异常)

    .NET客户端下载SQL Server数据库中文件流保存的大电子文件方法(不会报内存溢出异常) 前段时间项目使用一次性读去SQL Server中保存的电子文件的文件流然后返回给客户端保存下载电子文件, ...

  8. 【转】Android中引入第三方Jar包的方法&lpar;java&period;lang&period;NoClassDefFoundError解决办法&rpar;

    原文网址:http://www.blogjava.net/anchor110/articles/355699.html 1.在工程下新建lib文件夹,将需要的第三方包拷贝进来.2.将引用的第三方包,添 ...

  9. Jmeter报内存溢出解决方案

    描述:wimdows环境,做上传图片接口测试,涉及图片合成和上传,图片采用base64编码.每1s启动200线程的时候,Jmeter报内存溢出错误. 解决方案: 1.修改jmeter.bat: set ...

随机推荐

  1. Win10环境下安装theano并配置GPU详细教程

    一.软件和环境 (1)安装日期2016/12/23: (2)原材料VS2013,cuda-8.0(最好下载cuda7.5,目前theano-0.8.2对cuda-8支持不是很好),Anaconda3- ...

  2. oracle 分析函数的使用&lpar;1&rpar;

    LISTAGG(columnName,'拼接符') WITHIN GROUP(ORDER BY clause) --order by 子句决定拼接内容的顺序 LISTAGG(columnName,'' ...

  3. PLSQL 的简单命令之三

    -- 查找两个表中ID相等的 select a.id, a.name,b.math from stu a,scores b where a.id = b.id -- 右外连接 select b.id, ...

  4. 从零开始完整Electron桌面开发(1)搭建开发环境

    [OTC] # 需要知识 1. 简单的html.javascript.css知识,就是web前端入门知识. 2. 简单命令行的应用,不会也没关系,照着代码敲就行. 3. 下载安装就不说了吧. 4. 本 ...

  5. notification&period;setLatestEventInfo&lpar;context&comma; title&comma; message&comma; pendingIntent&rpar;&semi; undefined

    notification.setLatestEventInfo(context, title, message, pendingIntent);    在target为23时删除了该方法,我们应该使用 ...

  6. javascript之内置函数

    1.常规函数 (1)alert函数:显示一个警告对话框,包括一个OK按钮. (2)confirm函数:显示一个确认对话框,包括OK.Cancel按钮. (3)escape函数:将字符转换成Unicod ...

  7. Beta冲刺 1

    前言 队名:拖鞋旅游队 组长博客:https://www.cnblogs.com/Sulumer/p/10093150.html 作业博客:https://edu.cnblogs.com/campus ...

  8. Android的TextView设置加粗对汉字无效

    //not work textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD)); //work! static public voi ...

  9. afinalDb 用法

    研究Afinal是为了弄懂它到底是怎么实现的,它怎么就能够实现了呢?不过,现在先要看一下怎么用,再从表面推导内在. 本文就Afinal中建表.添加.删除.查找等常见数据库操作加以说明.探索. 一.创建 ...

  10. python 阿狸的进阶之路&lpar;8&rpar;

    异常处理 http://www.cnblogs.com/linhaifeng/articles/6232220.html(转) 网络编程socket http://www.cnblogs.com/li ...