美文网首页
Android多图片压缩

Android多图片压缩

作者: smallstrong | 来源:发表于2016-11-30 21:22 被阅读82次

获取手机本地图片地址,先进行尺寸压缩之后加载到内存,进行循环质量压缩,图片控制在200k大小(个人建议),将数据流以byte数组的形式直接丢给七牛,返回图片下载网址。

tips

  1. 选好照片之后返回的多张本地图片地址,开一个异步线程去循环压缩,这边用一个map集合,key是本地图片地址,value来存储byte数组。(单线程在压缩九张2M大小图片,耗时在10多秒左右,如果用多线程去解决循环压缩,会遇到几个潜在问题:瞬间的内存大量占用以及线程安全问题)。
  2. bitmap是非常吃内存的一个家伙,能确定不用到的时候请干翻他。
  3. 七牛提供本地资源地址上传于byte数组上传,个人建议用后者。比较读取本地资源会消耗更多资源且耗时。
  4. 当你选照片的时候一般都会有两个途径,一个是手机拍,另一个是直接选已有图片。为方便操作,手机拍好的图片之后保存到本地图库,再刷一遍本地相册,方便统一解决。

贴代码(图片压缩部分,七牛工具类就不贴了)

//尺寸压缩。当本地图片加载进内存的时候先压到接近屏幕大小。直接读取小心OOM
  public static Bitmap getScaleBitmap(Context ctx, String filePath) {
    BitmapFactory.Options opt = new BitmapFactory.Options();
    opt.inJustDecodeBounds = true;
    Bitmap bmp = BitmapFactory.decodeFile(filePath, opt);

    int bmpWidth = opt.outWidth;
    int bmpHeght = opt.outHeight;

    WindowManager windowManager = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
    Display display = windowManager.getDefaultDisplay();
    int screenWidth = display.getWidth();
    int screenHeight = display.getHeight();

    opt.inSampleSize = 1;
    if (bmpWidth > bmpHeght) {
        if (bmpWidth > screenWidth)
            opt.inSampleSize = bmpWidth / screenWidth;
    } else {
        if (bmpHeght > screenHeight)
            opt.inSampleSize = bmpHeght / screenHeight;
    }

    opt.inJustDecodeBounds = false;
    opt.inPreferredConfig = Bitmap.Config.RGB_565;
    opt.inPurgeable = true;// 同时设置才会有效
    opt.inInputShareable = true;
    bmp = BitmapFactory.decodeFile(filePath, opt);

    return bmp;
}


----------------------------------------------------------------


 // 循环质量压缩,压到200K放出来,以byte数组方式返回
    public static byte[] getBytes(Bitmap mBitmap, Context context) {
     ByteArrayOutputStream baos = new ByteArrayOutputStream();
     mBitmap.compress(Bitmap.CompressFormat.JPEG, 80, baos);
     int options = 80;
      while (baos.toByteArray().length / 1024 > 200) {
            Log.i("骑牛返回", baos.toByteArray().length + "");
          baos.reset();
          //每次都减少10
          options -= 10;
          //这里压缩options%,把压缩后的数据存放到baos中
          mBitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);
     }
        Log.i("骑牛返回","ok ahahah");
     mBitmap.recycle();
     System.gc();
     return baos.toByteArray();
  }    

相关文章

  • Android多图片压缩

    获取手机本地图片地址,先进行尺寸压缩之后加载到内存,进行循环质量压缩,图片控制在200k大小(个人建议),将数据流...

  • 图片压缩

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

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

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

  • 图片压缩

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

  • 【Android开发基础系列】图片专题

    1 图片编辑处理 1.1 图片裁切 转载自:bitmap的六种压缩方式,Android图片压缩 http://bl...

  • 资源图片优化

    tinypng 在线图片压缩 webP android studio选中图片右键Convert to webP

  • Android

    Android常用图片压缩方式 质量压缩 尺寸压缩 1. 质量压缩 质量压缩通过相应算法进行优化Bitmap的位深...

  • bitmap的六种压缩方式,Android图片压缩

    此处分享一个图片的压缩处理方式 : bitmap的六种压缩方式,Android图片压缩 转载链接,点击查看详情 !

  • Android图片压缩

    1.采样率压缩 采样率压缩是改变了图片的像素,他是通过先读取图片的边,然后在自己设定图片的边,然后根据设定,读取图...

  • android图片压缩

    质量压缩 尺寸压缩 缩放法压缩(matrix) RGB_565法(比ARGB_888少一半) createScal...

网友评论

      本文标题:Android多图片压缩

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