时间:2021-05-21
1. 通过设置采样率压缩
res资源图片压缩 decodeResource
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, resId, options); }uri图片压缩 decodeStream
public Bitmap decodeSampledBitmapFromUri(Uri uri, int reqWidth, 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), UtilUnitConversion.dip2px(MyApplication.mContext, reqHeight)); options.inJustDecodeBounds = false; bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options); } catch (Exception e) { e.printStackTrace(); } return bitmap; }本地File url图片压缩
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(), null, opts); opts.inDither = false; opts.inPurgeable = true; opts.inInputShareable = true; opts.inTempStorage = new byte[32 * 1024]; opts.inSampleSize = BitmapUtils.calculateInSampleSize(opts, UtilUnitConversion.dip2px(MyApplication.mContext, width), UtilUnitConversion.dip2px(MyApplication.mContext, height)); opts.inJustDecodeBounds = false; bitmap = BitmapFactory.decodeFileDescriptor(fs.getFD(), null, opts); } catch (IOException e) { e.printStackTrace(); } finally { if (null != fs) { try { fs.close(); } catch (IOException e) { e.printStackTrace(); } } } } } } return bitmap; }根据显示的图片大小进行SampleSize的计算
public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, 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; }调用方式:
复制代码 代码如下:
mImageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(), R.id.myImage, 100, 100))
2. 质量压缩:指定图片缩小到xkb以下
// 压缩到100kb以下 int maxSize = 100 * 1024; public static Bitmap getBitmapByte(Bitmap oriBitmap, int maxSize) { ByteArrayOutputStream out = new ByteArrayOutputStream(); oriBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); byte[] fileBytes = out.toByteArray(); int be = (maxSize * 100) / fileBytes.length; if (be > 100) { be = 100; } out.reset(); oriBitmap.compress(Bitmap.CompressFormat.JPEG, be, out); return oriBitmap; }3. 单纯获取图片宽高避免oom的办法
itmapFactory.Options这个类,有一个字段叫做 inJustDecodeBounds 。SDK中对这个成员的说明是这样的:
If set to true, the decoder will return null (no bitmap), but the out...
也就是说,如果我们把它设为true,那么BitmapFactory.decodeFile(String path, Options opt)并不会真的返回一个Bitmap给你,它仅仅会把它的宽,高取回来给你,这样就不会占用太多的内存,也就不会那么频繁的发生OOM了。
/** * 根据res获取Options,来获取宽高outWidth和options.outHeight * @param res * @param resId * @return */ public static BitmapFactory.Options decodeOptionsFromResource(Resources res, int resId) { // First decode with inJustDecodeBounds=true to check dimensions final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(res, resId, options); return options; }以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
Android设备的内存有限,对于大图片,必须进行压缩后再进行显示,否则会出现内存溢出:OOM;处理策略:1.使用缩略图(Thumbnails);Android
本文实例总结了Android编程之内存溢出解决方案(OOM)。分享给大家供大家参考,具体如下:在最近做的工程中发现加载的图片太多或图片过大时经常出现OOM问题,
大家好,今天给大家分享的是解决解析图片的出现oom的问题,我们可以用BitmapFactory这里的各种Decode方法,如果图片很小的话,不会出现oom,但是
Android图片压缩几种方式总结图片压缩在Android开发中很常见也很重要,防止图片的OOM也是压缩的重要原因。首先看下Bitmap图片文件的大小的决定因素
整理文档,搜刮出一个Android图片实现压缩处理的实例代码,稍微整理精简一下做下分享。详解:1.获取本地图片File文件获取BitmapFac