Android设备的内存有限,对于大图片,必须进行压缩后再进行显示,否则会出现内存溢出:OOM;
处理策略:
1.使用缩略图(Thumbnails);
Android系统会给检测到的图片创建缩略图;可以操作Media内容提供者中的Image对图片进行操作;
2.手动压缩:
以下是自己整理的小工具类(对于按比例缩放后,在此并未再进行质量缩放,此时图片大小有可能超出我们期望的限制;假如我们有严格的大小限制需求,可先进行按比例缩放后,判断此时图片大小是否超出限制;如果超出限制,对其再进行质量缩放即可。建议使用按比例缩放,按质量缩放很有可能导致图片失真。)
</pre><p><pre name="code" class="java">package com.util; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import android.graphics.Bitmap; import android.graphics.Matrix; import android.graphics.Bitmap.CompressFormat; import android.graphics.BitmapFactory; import android.media.ExifInterface; /** * 图片压缩工具类 * @author 丶Life_ */ public class ImageCompressUtil { /** * 通过降低图片的质量来压缩图片 * @param bmp * 要压缩的图片位图对象 * @param maxSize * 压缩后图片大小的最大值,单位KB * @return 压缩后的图片位图对象 */ public static Bitmap compressByQuality(Bitmap bitmap, int maxSize) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); int quality = 100; bitmap.compress(CompressFormat.JPEG, quality, baos); System.out.println("图片压缩前大小:" + baos.toByteArray().length + "byte"); boolean isCompressed = false; while (baos.toByteArray().length / 1024 > maxSize) { quality -= 10; baos.reset(); bitmap.compress(CompressFormat.JPEG, quality, baos); System.out.println("质量压缩到原来的" + quality + "%时大小为:" + baos.toByteArray().length + "byte"); isCompressed = true; } System.out.println("图片压缩后大小:" + baos.toByteArray().length + "byte"); if (isCompressed) { Bitmap compressedBitmap = BitmapFactory.decodeByteArray( baos.toByteArray(), 0, baos.toByteArray().length); recycleBitmap(bitmap); return compressedBitmap; } else { return bitmap; } } /** * 传入图片url,通过压缩图片的尺寸来压缩图片大小 * @param pathName 图片的完整路径 * @param targetWidth 缩放的目标宽度 * @param targetHeight 缩放的目标高度 * @return 缩放后的图片 */ public static Bitmap compressBySize(String pathName, int targetWidth, int targetHeight) { BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true;// 不去真的解析图片,只是获取图片的头部信息,包含宽高等; Bitmap bitmap = BitmapFactory.decodeFile(pathName, opts); // 得到图片的宽度、高度; int imgWidth = opts.outWidth; int imgHeight = opts.outHeight; // 分别计算图片宽度、高度与目标宽度、高度的比例;取大于等于该比例的最小整数; int widthRatio = (int) Math.ceil(imgWidth / (float) targetWidth); int heightRatio = (int) Math.ceil(imgHeight / (float) targetHeight); if (widthRatio > 1 || heightRatio > 1) { if (widthRatio > heightRatio) { opts.inSampleSize = widthRatio; } else { opts.inSampleSize = heightRatio; } } // 设置好缩放比例后,加载图片进内容; opts.inJustDecodeBounds = false; bitmap = BitmapFactory.decodeFile(pathName, opts); return bitmap; } /** * 传入bitmap,通过压缩图片的尺寸来压缩图片大小 * @param bitmap 要压缩图片 * @param targetWidth 缩放的目标宽度 * @param targetHeight 缩放的目标高度 * @return 缩放后的图片 */ public static Bitmap compressBySize(Bitmap bitmap, int targetWidth, int targetHeight) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(CompressFormat.JPEG, 100, baos); BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true; bitmap = BitmapFactory.decodeByteArray(baos.toByteArray(), 0, baos.toByteArray().length, opts); // 得到图片的宽度、高度; int imgWidth = opts.outWidth; int imgHeight = opts.outHeight; // 分别计算图片宽度、高度与目标宽度、高度的比例;取大于该比例的最小整数; int widthRatio = (int) Math.ceil(imgWidth / (float) targetWidth); int heightRatio = (int) Math.ceil(imgHeight / (float) targetHeight); if (widthRatio > 1 || heightRatio > 1) { if (widthRatio > heightRatio) { opts.inSampleSize = widthRatio; } else { opts.inSampleSize = heightRatio; } } // 设置好缩放比例后,加载图片进内存; opts.inJustDecodeBounds = false; Bitmap compressedBitmap = BitmapFactory.decodeByteArray( baos.toByteArray(), 0, baos.toByteArray().length, opts); recycleBitmap(bitmap); return compressedBitmap; } /** * 通过压缩图片的尺寸来压缩图片大小,通过读入流的方式,可以有效防止网络图片数据流形成位图对象时内存过大的问题; * @param InputStream 要压缩图片,以流的形式传入 * @param targetWidth 缩放的目标宽度 * @param targetHeight 缩放的目标高度 * @return 缩放后的图片 * @throws IOException 读输入流的时候发生异常 */ public static Bitmap compressBySize(InputStream is, int targetWidth, int targetHeight) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buff = new byte[1024]; int len = 0; while ((len = is.read(buff)) != -1) { baos.write(buff, 0, len); } byte[] data = baos.toByteArray(); BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opts); // 得到图片的宽度、高度; int imgWidth = opts.outWidth; int imgHeight = opts.outHeight; // 分别计算图片宽度、高度与目标宽度、高度的比例;取大于该比例的最小整数; int widthRatio = (int) Math.ceil(imgWidth / (float) targetWidth); int heightRatio = (int) Math.ceil(imgHeight / (float) targetHeight); if (widthRatio > 1 || heightRatio > 1) { if (widthRatio > heightRatio) { opts.inSampleSize = widthRatio; } else { opts.inSampleSize = heightRatio; } } // 设置好缩放比例后,加载图片进内存; opts.inJustDecodeBounds = false; bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opts); return bitmap; } /** * 旋转图片摆正显示 * @param srcPath * @param bitmap * @return */ public static Bitmap rotateBitmapByExif(String srcPath, Bitmap bitmap) { ExifInterface exif; Bitmap newBitmap = null; try { exif = new ExifInterface(srcPath); if (exif != null) { // 读取图片中相机方向信息 int ori = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); int digree = 0; switch (ori) { case ExifInterface.ORIENTATION_ROTATE_90: digree = 90; break; case ExifInterface.ORIENTATION_ROTATE_180: digree = 180; break; case ExifInterface.ORIENTATION_ROTATE_270: digree = 270; break; } if (digree != 0) { Matrix m = new Matrix(); m.postRotate(digree); newBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true); recycleBitmap(bitmap); return newBitmap; } } } catch (IOException e) { e.printStackTrace(); } return bitmap; } /** * 回收位图对象 * @param bitmap */ public static void recycleBitmap(Bitmap bitmap) { if (bitmap != null && !bitmap.isRecycled()) { bitmap.recycle(); System.gc(); bitmap = null; } } }
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对小牛知识库的支持。如果你想了解更多相关内容请查看下面相关链接
本文向大家介绍java图片压缩工具类,包括了java图片压缩工具类的使用技巧和注意事项,需要的朋友参考一下 直接上java图片压缩code: 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。
主要内容:压实问题我们知道动态分区受到外部碎片的影响。 但是,这可能会导致一些严重的问题。 为了避免压缩,我们需要更改规则,该规则指出进程无法存储在内存中的不同位置。 也可以使用压缩来减少外部碎片的可能性。 在压缩过程中,所有的空闲分区都是连续的,所有加载的分区都集中在一起。 通过应用这种技术,可以将更大的进程存储在内存中。 合并可用分区,现在可以根据新进程的需要分配这些分区。 这种技术也称为碎片整理。 如上图所示
本文向大家介绍Android图片压缩(质量压缩和尺寸压缩),包括了Android图片压缩(质量压缩和尺寸压缩)的使用技巧和注意事项,需要的朋友参考一下 在网上调查了图片压缩的方法并实装后,大致上可以认为有两类压缩:质量压缩(不改变图片的尺寸)和尺寸压缩(相当于是像素上的压缩);质量压缩一般可用于上传大图前的处理,这样就可以节省一定的流量,毕竟现在的手机拍照都能达到3M左右了,尺寸压缩一般可用于生成
html 单页面项目,用什么压缩工具来压缩图片,css,js好呢? 项目是通过cdn引入的vue ,还引入了一些本地的css,js,图片,现在项目要上线,想提高加载速度压缩一下,用什么工具好?一个一个这样压缩就算了。
本文向大家介绍Android中3种图片压缩处理方法,包括了Android中3种图片压缩处理方法的使用技巧和注意事项,需要的朋友参考一下 Android中图片的存在形式: 1:文件形式:二进制形式存在与硬盘中。 2:流的形式:二进制形式存在与内存中。 3:Bitmap的形式 三种形式的区别: 文件形式和流的形式:对图片体积大小并没有影响。也就是说,如果你手机SD卡上的图片通过流的形式读到内存中,在内
compressImage 压缩图片接口(安卓10.0.12版本支持,ios 10.0.15版本支持) 使用方法 AlipayJSBridge.call('compressImage', { apFilePaths: ["https://resource/apmlcc0ed184daffc5a0d8da86b2f518cf7b.image"], compressLevel: 4 }, f