美文网首页Android开发
Android异步批量压缩图片

Android异步批量压缩图片

作者: 在寻找雪见的景天 | 来源:发表于2016-08-26 11:11 被阅读1345次

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

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

List<String> 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<String> 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<String> fileList = new ArrayList<>();
    private ProgressDialog progressDialog;

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

    class CompressTask extends AsyncTask<Void, Integer, Integer> {
        private Context context;
        private List<String> list;
        private CompressCallBack callBack;

        CompressTask(Context context, List<String> 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<String> list);
    }

}


相关文章

  • Android异步批量压缩图片

    最近稍微空闲了一点,然后开始整理一下之前项目用到的东西,方便以后项目再次使用。很多项目需要用到发布图片的功能吧,像...

  • Webp图片批处理程序

    功能 1、批量将PNG/JPEG格式图片转换Webp格式图片2、批量将Webp格式图片再压缩【压缩效率0-100可...

  • pngquant图片压缩

    pngquant pngquant可以通过命令行压缩png图片。 脚本批量压缩png图片 quality表示压缩的...

  • iOS优化-瘦身

    图片 1、批量压缩图片(tinyPNG) 用TinyPNG[https://tinypng.com/]将图片进行压...

  • webpack-imgmini,一个运用webpack批量压缩图

    运用webpack批量压缩图片的小工具,支持png,jpg,gif,对大图片压缩效果较好 项目地址:https:/...

  • 技术杂货铺

    统计工程中类个数 2.压缩工程中png图片 3.测试APP启动时间 4.批量删除git分支 Android 命令查...

  • iOS 优化

    资源优化:对资源文件下手,压缩图片/音频,去除不必要的资源iOS项目瘦身,删除无用图片,批量压缩图片 编译优化:r...

  • 图片压缩

    Android应用开发中三种常见的图片压缩方法,分别是:质量压缩法、比例压缩法(根据路径获取图片并压缩)和比例压缩...

  • Android:图片压缩的几种方式

    1、前言 在Android中,图片的压缩对于内存的优化很重要 通过这篇文章对Android中的几种图片压缩方式进行...

  • 图片压缩

    Android图片压缩常用的有质量压缩、尺寸压缩、采样率压缩以及通过JNI调用libjpeg库来进行压缩(尺寸压缩...

网友评论

    本文标题:Android异步批量压缩图片

    本文链接:https://www.haomeiwen.com/subject/ytxzsttx.html