Android图片压缩

来源:互联网 发布:紫 蔡健雅 知乎 编辑:程序博客网 时间:2024/06/05 08:51
package com.test;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import android.annotation.SuppressLint;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Bundle;import android.os.Environment;import android.util.Log;import android.widget.ImageView;public class TestCompressBitmap extends Activity {/** * 使用说明: * 1、二个方法,一个方法用于将全路径转换成inputstream,另外一个方法将图片的质量缩小 *//* 变量申明  */private ImageView image1;private ImageView image2;@SuppressLint("NewApi")public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);image1 = (ImageView) findViewById(R.id.image1);image2 = (ImageView) findViewById(R.id.image2);//image1显示原始图片try {InputStream is = getImageInputstream("/mnt/sdcard/test.jpg");Bitmap bitmap1 = BitmapFactory.decodeStream(is);Log.v("11111111111111111111111", bitmap1.getByteCount() + "");image1.setImageBitmap(bitmap1);is.close();} catch (IOException e) {e.printStackTrace();}// ----------------------------------------------------------------------------------// 显示压缩图片try {InputStream is = getImageInputstream("/mnt/sdcard/test.jpg");//Bitmap bitmap2 = revitionImageSize(is, 200);Bitmap bitmap2 = revitionImageSize("/mnt/sdcard/test.jpg", 200);Log.v("22222222222222222222222", bitmap2.getByteCount() + "");image2.setImageBitmap(bitmap2);is.close();} catch (Exception e) {e.printStackTrace();}}/* 将SD卡中的图片路径转换成inpustream */public InputStream getImageInputstream(String filePath){InputStream fileInputStream = null;try {/* 如果SD卡有挂载才执行,防止-- */if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {File file = new File(filePath);if(file.exists()){fileInputStream = new FileInputStream(file);}}} catch (Exception e) {e.printStackTrace();}return fileInputStream;}/* 将输入流转成Bitmap返回 */private Bitmap revitionImageSize(InputStream is, int size) throws IOException {// 取得图片BitmapFactory.Options options = new BitmapFactory.Options();// 这个参数代表,不为bitmap分配内存空间,只记录一些该图片的信息(例如图片大小),说白了就是为了内存优化options.inJustDecodeBounds = true;// 通过创建图片的方式,取得options的内容(这里就是利用了java的地址传递来赋值)BitmapFactory.decodeStream(is, null, options);// 生成压缩的图片int i = 0;Bitmap bitmap = null;while (true) {// 这一步是根据要设置的大小,使宽和高都能满足if ((options.outWidth >> i <= size)&& (options.outHeight >> i <= size)) {// 重新取得流,注意:这里一定要再次加载,不能二次使用之前的流!// 这个参数表示 新生成的图片为原始图片的几分之一。options.inSampleSize = (int) Math.pow(2.0D, i);// 这里之前设置为了true,所以要改为false,否则就创建不出图片options.inJustDecodeBounds = false;bitmap = BitmapFactory.decodeStream(is, null, options);break;}i += 1;}return bitmap;}/**/private Bitmap revitionImageSize(String path, int size) throws IOException {// 取得图片//InputStream temp = this.getAssets().open(path);InputStream temp = getImageInputstream(path);BitmapFactory.Options options = new BitmapFactory.Options();// 这个参数代表,不为bitmap分配内存空间,只记录一些该图片的信息(例如图片大小),说白了就是为了内存优化options.inJustDecodeBounds = true;// 通过创建图片的方式,取得options的内容(这里就是利用了java的地址传递来赋值)BitmapFactory.decodeStream(temp, null, options);// 关闭流temp.close();// 生成压缩的图片int i = 0;Bitmap bitmap = null;while (true) {// 这一步是根据要设置的大小,使宽和高都能满足if ((options.outWidth >> i <= size)&& (options.outHeight >> i <= size)) {// 重新取得流,注意:这里一定要再次加载,不能二次使用之前的流!temp = getImageInputstream(path);// 这个参数表示 新生成的图片为原始图片的几分之一。options.inSampleSize = (int) Math.pow(2.0D, i);// 这里之前设置为了true,所以要改为false,否则就创建不出图片options.inJustDecodeBounds = false;bitmap = BitmapFactory.decodeStream(temp, null, options);break;}i += 1;}return bitmap;}}

0 0