Android设备的内存有限,对于大图片,必须进行压缩后再进行显示,否则会出现内存溢出:OOM;
处理大图片:
1.使用缩略图(Thumbnails);
Android系统会给检测到的图片创建缩略图;可以操作Media内容提供者中的Image对图片进行操作;
2.手动压缩:
(1)根据图片和屏幕尺寸,等比压缩,完美显示;
(2)降低图片质量,压缩图片大小;
以下是自己整理的小工具类(对于按比例缩放后,在此并未再进行质量缩放,此时图片大小有可能超出我们期望的限制;假如我们有严格的大小限制需求,可先进行按比例缩放后,判断此时图片大小是否超出限制;如果超出限制,对其再进行质量缩放即可。建议使用按比例缩放,按质量缩放很有可能导致图片失真。)
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import android.graphics.Bitmap;
- import android.graphics.Bitmap.CompressFormat;
- import android.graphics.BitmapFactory;
- /**
- * 图片压缩工具类
- * @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");
- while (baos.toByteArray().length / 1024 > maxSize) {
- quality -= 10;
- baos.reset();
- bitmap.compress(CompressFormat.JPEG, quality, baos);
- System.out.println("质量压缩到原来的" + quality + "%时大小为:"
- + baos.toByteArray().length + "byte");
- }
- System.out.println("图片压缩后大小:" + baos.toByteArray().length + "byte");
- bitmap = BitmapFactory.decodeByteArray(baos.toByteArray(), 0,
- baos.toByteArray().length);
- return bitmap;
- }
- /**
- * 通过压缩图片的尺寸来压缩图片大小
- *
- * @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 || widthRatio > 1) {
- if (widthRatio > heightRatio) {
- opts.inSampleSize = widthRatio;
- } else {
- opts.inSampleSize = heightRatio;
- }
- }
- // 设置好缩放比例后,加载图片进内容;
- opts.inJustDecodeBounds = false;
- bitmap = BitmapFactory.decodeFile(pathName, opts);
- return 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 && widthRatio > 1) {
- if (widthRatio > heightRatio) {
- opts.inSampleSize = widthRatio;
- } else {
- opts.inSampleSize = heightRatio;
- }
- }
- // 设置好缩放比例后,加载图片进内存;
- opts.inJustDecodeBounds = false;
- bitmap = BitmapFactory.decodeByteArray(baos.toByteArray(), 0,
- baos.toByteArray().length, opts);
- return bitmap;
- }
- /**
- * 通过压缩图片的尺寸来压缩图片大小,通过读入流的方式,可以有效防止网络图片数据流形成位图对象时内存过大的问题;
- *
- * @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 && widthRatio > 1) {
- if (widthRatio > heightRatio) {
- opts.inSampleSize = widthRatio;
- } else {
- opts.inSampleSize = heightRatio;
- }
- }
- // 设置好缩放比例后,加载图片进内存;
- opts.inJustDecodeBounds = false;
- bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opts);
- return bitmap;
- }
- }