图片压缩

  1. //对图片进行压缩  
  2.            BitmapFactory.Options options = new BitmapFactory.Options();  
  3.            options.inJustDecodeBounds = true;  
  4.              
  5.            //获取这个图片的宽和高  
  6.            Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/dcim/Camera/hello.jpg",options);//此时返回bm为空  
  7.            options.inJustDecodeBounds =false;  
  8.            //计算缩放比  
  9.            int be = (int)(options.outHeight / (float)200);  
  10.            if(be <= 0)  
  11.                 be =1;  
  12.            options.inSampleSize =be;  
  13.            //重新读入图片,注意这次要把options.inJustDecodeBounds设为false哦  
  14.            bitmap = BitmapFactory.decodeFile("/sdcard/dcim/Camera/hello.jpg",options);  
  15.            int w = bitmap.getWidth();  
  16.            int h=bitmap.getHeight();  
  17.            System.out.println(w+" "+h);  
  18.            myImageView.setImageBitmap(bitmap);  
  19.              
  20.              
  21.            //保存入sdCard  
  22.            File file2= new File("/sdcard/dcim/Camera/test.jpg");  
  23.            try {  
  24.             FileOutputStream out = new FileOutputStream(file2);  
  25.             if(bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)){  
  26.                 out.flush();  
  27.                 out.close();  
  28.             }  
  29.         } catch (Exception e) {  
  30.             // TODO: handle exception  
  31.         }  
  32.              
  33.              
  34.         //读取sd卡  
  35.            File file =new File("/sdcard/dcim/Camera/test.jpg");  
  36.            int maxBufferSize = 16 * 1024;  
  37.               
  38.             int len = 0;  
  39.             ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
  40.              BufferedInputStream bufferedInputStream;  
  41.             try {  
  42.                 bufferedInputStream = new BufferedInputStream(new FileInputStream(file));  
  43.                 int bytesAvailable = bufferedInputStream.available();  
  44.                 int bufferSize = Math.min(bytesAvailable, maxBufferSize);  
  45.                 byte[] buffer = new byte[bufferSize];  
  46.                 while ((len = bufferedInputStream.read(buffer)) != -1)  
  47.                 {  
  48.                     outStream.write(buffer, 0, bufferSize);  
  49.                 }  
  50.                  data = outStream.toByteArray();  
  51.                 outStream.close();  
  52.                 bufferedInputStream.close();  
  53.                   
  54.             } catch (FileNotFoundException e) {  
  55.                 e.printStackTrace();  
  56.             } catch (IOException e) {  
  57.                 e.printStackTrace();  
  58.             }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值