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(" ...

  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. .NET客户端下载SQL Server数据库中文件流保存的大电子文件方法(不会报内存溢出异常)

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

  8. 【转】Android中引入第三方Jar包的方法(java.lang.NoClassDefFoundError解决办法)

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

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

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

随机推荐

  1. android自定义控件(5)-实现ViewPager效果

    对于系统的ViewGroup我们已经是十分熟悉了,最常用的LinearLayout和RelativeLayout几乎是天天要打交道,下面我们就来看看,如何一步一步将其实现: 一.首先当然也是最通常的新 ...

  2. SVN Tree Conflict 的分析

    所谓Tree Confict,就是至少有一个人修改了目录结构,包括文件或者文件所在目录的改名.删除.移动.然后Update或Merge的时候就报了Tree Conflict. 介绍一下概念Delete ...

  3. svn三大目录trunk、branch和tag

    SVN中Branch和tag的比较在SVN中Branch和tag在一个功能选项中,在使用中也往往产生混淆.在实现上,branch和tag,对于svn都是使用copy实现的,所以他们在默认的权限上和一般 ...

  4. 关于Mysql错误:./bin/mysqld_safe --user=mysql& [1] 32710 121003 16:40:22 mysqld_safe Logging to '/var/log/mysqld.log'. 121003 16:40:22 mysqld_s

    [root@www]# ./bin/mysqld_safe --user=mysql&[1] 32710[root@www]# 121003 16:40:22 mysqld_safe Logg ...

  5. C/C++使用HTTP协议上传

    上传文件: http://zengrong.net/post/2088.htm #include <stdio.h> #include <string.h> #include ...

  6. 采用ETL with RDBMS模式来实现ETL

    目前Teradata数据仓库的ETL作业采用ELT方式, 因为loading太重了, 需要将ETL压力转移到专门的ETL Server上. 对于ETL工具, 市场上已有很成熟的商业/开源工具, 比如I ...

  7. 更改vs自带的模板

    路径:C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ItemTemplatesCache\CSharp\Code\20 ...

  8. WCF--安全小见解...

    由于WCF写的服务需要Ajax来进行调用(这个配置过程也是一个比较咕~~(╯﹏╰)b的经历...), 所以调用的过程都是前台可以看到的,不加点安全措施上去,真的像是一个裸奔在互联网上的接口... 反正 ...

  9. VS2010创建动态链接库并且使用动态链接库DLL

    1.编写动态链接库文件 dll和lib文件 例子: 在新建VS工程时选择DLL 空项目 ----------hello.h-------- #include <stdio.h> #prag ...

  10. 如何在CentOS 7上安装EPEL源

    EPEL 是什么? EPEL (Extra Packages for Enterprise Linux,企业版Linux的额外软件包) 是Fedora小组维护的一个软件仓库项目,为RHEL/CentO ...