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

本文介绍了一种优化Android应用中图片加载速度与节省内存的方法,通过调整图片大小和质量,实现高效的图片处理与存储。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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”同学的分享。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值