Android异步批量压缩图片

最近稍微空闲了一点,然后开始整理一下之前项目用到的东西,方便以后项目再次使用。很多项目需要用到发布图片的功能吧,像社区朋友圈之类的,如果直接把图片不经过压缩上传,那体验肯定不好,第一个浪费流量、第二个等待的时间太长。所以上传前还是来压缩一下照片吧,这里压缩是对尺寸和质量进行了压缩,压缩后的照片在100k左右。保证了清晰度同时体积也大大减少。

废话不多说,直接来看使用方法:

List list = new ArrayList<>();
        list.add("mnt/sdcard/1.jpg");
        list.add("mnt/sdcard/2.jpg");
        list.add("mnt/sdcard/3.jpg");
        new CompressPhotoUtils().CompressPhoto(MainActivity.this, list, new CompressCallBack() {

            @Override
            public void success(List list) {
                //upload(list);执行上传的方法
            }
        });

是不是很简单,调用方法后,会使用异步任务来压缩图片,回调的list集合就是压缩完的照片路径集合,在这里面调用上传的方法就行了。
最后附上代码:

package com.example.lol;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;

public class CompressPhotoUtils {

    private List fileList = new ArrayList<>();
    private ProgressDialog progressDialog;

    public void CompressPhoto(Context context, List list, CompressCallBack callBack) {
        CompressTask task = new CompressTask(context, list, callBack);
        task.execute();
    }

    class CompressTask extends AsyncTask {
        private Context context;
        private List list;
        private CompressCallBack callBack;

        CompressTask(Context context, List list, CompressCallBack callBack) {
            this.context = context;
            this.list = list;
            this.callBack = callBack;
        }

        /**
         * 运行在UI线程中,在调用doInBackground()之前执行
         */
        @Override
        protected void onPreExecute() {
            progressDialog = ProgressDialog.show(context, null, "处理中...");
        }

        /**
         * 后台运行的方法,可以运行非UI线程,可以执行耗时的方法
         */
        @Override
        protected Integer doInBackground(Void... params) {
            for (int i = 0; i < list.size(); i++) {
                Bitmap bitmap = getBitmap(list.get(i));
                String path = SaveBitmap(bitmap, i);
                fileList.add(path);
            }
            return null;
        }

        /**
         * 运行在ui线程中,在doInBackground()执行完毕后执行
         */
        @Override
        protected void onPostExecute(Integer integer) {
            progressDialog.dismiss();
            callBack.success(fileList);
        }

        /**
         * 在publishProgress()被调用以后执行,publishProgress()用于更新进度
         */
        @Override
        protected void onProgressUpdate(Integer... values) {
        }
    }

    /**
     * 从sd卡获取压缩图片bitmap
     */
    public static Bitmap getBitmap(String srcPath) {
        BitmapFactory.Options newOpts = new BitmapFactory.Options();
        newOpts.inJustDecodeBounds = true;
        Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
        newOpts.inJustDecodeBounds = false;
        int w = newOpts.outWidth;
        int h = newOpts.outHeight;
        float hh = 1280f;
        float ww = 720f;
        // 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
        int be = 1;// be=1表示不缩放
        if (w > h && w > ww) {// 如果宽度大的话根据宽度固定大小缩放
            be = (int) (newOpts.outWidth / ww);
        } else if (w < h && h > hh) {// 如果高度高的话根据宽度固定大小缩放
            be = (int) (newOpts.outHeight / hh);
        }
        newOpts.inSampleSize = be;// 设置缩放比例
        bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
        return bitmap;
    }

    /**
     * 保存bitmap到内存卡
     */
    public static String SaveBitmap(Bitmap bmp, int num) {
        File file = new File("mnt/sdcard/贝贝宠/");
        String path = null;
        if (!file.exists())
            file.mkdirs();
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String picName = formatter.format(new Date());
            path = file.getPath() + "/" + picName + "-" + num + ".jpg";
            FileOutputStream fileOutputStream = new FileOutputStream(path);
            bmp.compress(Bitmap.CompressFormat.JPEG, 90, fileOutputStream);
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return path;
    }

    public interface CompressCallBack {
        void success(List list);
    }

}


你可能感兴趣的:(Android异步批量压缩图片)