Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Android 图片处理避免出现oom的方法详解大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

1. 通过设置采样率压缩

res资源图片压缩 decoderesource

@H_772_7@ public Bitmap decodeSampledBitmapFromresource(resources res,int resId,int reqWidth,int reqHeight) { // First decode with inJustDecodeBounds=true to check dimensions final BitmapFactory.options options = new BitmapFactory.options(); options.inJustDecodeBounds = true; BitmapFactory.decoderesource(res,resId,options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options,reqWidth,reqHeight); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decoderesource(res,options); }

uri图片压缩 decodeStream

@H_772_7@ public Bitmap decodeSampledBitmapFromUri(Uri uri,int reqHeight) { Bitmap bitmap = null; try { BitmapFactory.options options = new BitmapFactory.options(); options.inJustDecodeBounds = true; BitmapFactory.decodeStream(getContentResolver().openInputStream(uri),null,options); options.inSampleSize = BitmapUtils.calculateInSampleSize(options,UtilUnitConversion.dip2px(MyApplication.mContext,reqWidth),reqHeight)); options.inJustDecodeBounds = false; bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri),options); } catch (Exception E) { e.printStackTrace(); } return bitmap; }

本地File url图片压缩

@H_772_7@ public static Bitmap getloadlBitmap(String load_url,int width,int height) { Bitmap bitmap = null; if (!UtilText.isEmpty(load_url)) { File file = new File(load_url); if (file.exists()) { FileInputStream fs = null; try { fs = new FileInputStream(filE); } catch (FileNotFoundException E) { e.printStackTrace(); } if (null != fs) { try { BitmapFactory.options opts = new BitmapFactory.options(); opts.inJustDecodeBounds = true; BitmapFactory.decodeFileDescriptor(fs.getFD(),opts); opts.inDither = false; opts.inPurgeable = true; opts.inInputShareable = true; opts.inTempStorage = new byte[32 * 1024]; opts.inSampleSize = BitmapUtils.calculateInSampleSize(opts,width),height)); opts.inJustDecodeBounds = false; bitmap = BitmapFactory.decodeFileDescriptor(fs.getFD(),opts); } catch (IOException E) { e.printStackTrace(); } finally { if (null != fs) { try { fs.close(); } catch (IOException E) { e.printStackTrace(); } } } } } } return bitmap; }

根据显示图片大小进行SampleSize的计算

@H_772_7@ public int calculateInSampleSize(BitmapFactory.options options,int reqHeight) { if (reqWidth == 0 || reqHeight == 0) { return 1; } // Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { final int halfHeight = height / 2; final int halfWidth = width / 2; // Calculate the largest inSampleSize value that is a power of 2 and // keeps both height and width larger than the requested height and width. while ((halfHeight / inSampleSizE) >= reqHeight && (halfWidth / inSampleSizE) >= reqWidth) { inSampleSize *= 2; } } return inSampleSize; }

调用方式:

@H_403_31@复制代码 代码如下:

大佬总结

以上是大佬教程为你收集整理的Android 图片处理避免出现oom的方法详解全部内容,希望文章能够帮你解决Android 图片处理避免出现oom的方法详解所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。