简单粗暴的图片压缩,可以压到100kb以内

public static Bitmap revitionImageSize(String path) throws IOException {
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(path)));
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(in, null, options);
        in.close();
//      int i = 0;
        Bitmap bitmap = null;

        int wRatio = (int) Math.ceil(options.outWidth / (float) 720); 
        int hRatio = (int) Math.ceil(options.outHeight / (float) 1280); 

        if (wRatio > 1 && hRatio > 1) {  
            if (wRatio > hRatio) {  
                options.inSampleSize = wRatio;  
            } else {  
                options.inSampleSize = hRatio;  
            }  
        }  

        in = new BufferedInputStream(new FileInputStream(new File(path)));
        options.inJustDecodeBounds = false;

        // 杯具的老戳手机-1G以下的内存的某些手机无法加载高清图片大于1M以上,只能加大压缩力度
        try{
            bitmap = BitmapFactory.decodeStream(in, null, options);
        }catch (Exception e) {
            e.printStackTrace();

            wRatio = (int) Math.ceil(options.outWidth / (float) 480); 
            hRatio = (int) Math.ceil(options.outHeight / (float) 800); 

            if (wRatio > 1 && hRatio > 1) {  
                if (wRatio > hRatio) {  
                    options.inSampleSize = wRatio;  
                } else {  
                    options.inSampleSize = hRatio;  
                }  
            }  

            in = new BufferedInputStream(new FileInputStream(new File(path)));
            options.inJustDecodeBounds = false;

            bitmap = BitmapFactory.decodeStream(in, null, options);
        }

//      while (true) {
//          if ((options.outWidth >> i <= 1000)
//                  && (options.outHeight >> i <= 1000)) {
//              in = new BufferedInputStream(
//                      new FileInputStream(new File(path)));
//              options.inSampleSize = (int) Math.pow(2.0D, i);
//              options.inJustDecodeBounds = false;
//              bitmap = BitmapFactory.decodeStream(in, null, options);
//              break;
//          }
//          i += 1;
//      }
        return bitmap;
    }




******************************
然后图片以质量80来保存,这样的组合是最优之一
我从像素的1600*1400调到  480*640 ,  然后质量从10020都一 一的尝试过,最优之一的组合是 1280*720 , 质量80
******************************

public static String SDPATH = Environment.getExternalStorageDirectory()
            + "/tempMicroPet/";

    public static void saveBitmap(Bitmap bm, String picName) {
        //Log.e("", "保存图片");
        try {
            if (!isFileExist("")) {
                File tempf = createSDDir("");
            }
            File f = new File(SDPATH, picName + ".JPEG"); 
            if (f.exists()) {
                f.delete();
            }
            FileOutputStream out = new FileOutputStream(f);
            bm.compress(Bitmap.CompressFormat.JPEG, 80, out);
            out.flush();
            out.close();
            //Log.e("", "已经保存");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

注:以上代码来自Android-Lite群(47357508)的“就看Liter”同学的分享。

你可能感兴趣的:(Android点滴)