首页 技术 正文
技术 2022年11月6日
0 收藏 608 点赞 1,198 浏览 2536 个字

  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);
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291