Android 照片压缩

今天给大家带来的是Android照片压缩

 

首先在xml里建个button(拍照)和一个fill的imageview 用来展示压缩好的图片

拍照的onclicklistener点击事件

Intent intent = new Intent();
String localDir = "pics";// 照片保存地址
localFielName = DateFormat.format("yyyyMMddhhmmss", new Date()).toString() + ".jpg";//图片保存的名字
dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + localDir);//手机内存的pics
if (!dir.exists())//如果文件夹不存在,创建
   dir.mkdirs();
File f = new File(dir, localFielName);
imageUri = Uri.fromFile(f);
try {
   courseFile = f.getCanonicalPath();// 获取文件路径
} catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
}
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//添加相机的外部启用
intent.addCategory(Intent.CATEGORY_DEFAULT);//应用启动默认的第一个启动的activity
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);//将照片存储两次,分别是系统和自己创建的
startActivityForResult(intent, 0);

注释写的很清楚

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   //第一个参数为请求码,即调用startActivityForResult()传递过去的值
   //第二个参数为请求码,可以根据业务需求自己编号
   //第二个参数为结果码,结果码用于标识返回数据来自哪个新Activity
   // TODO Auto-generated method stub
   if (resultCode == 0) {

   } else {
      if (requestCode == 0) {
         compressPic(courseFile);
      }
   }
}

public void compressPic(String filepath) {
   Bitmap bitMap = BitmapFactory.decodeFile(filepath);
   Bitmap bitmapFirst = BitmapCompressor.decodeSampledBitmapFromFile(courseFile, 320, 240);
   img.setImageBitmap(bitmapFirst);//imageView 展示图片
   File file = new File(courseFile);
   file.delete();//删除原图
   BitmapCompressor.saveBitmap2file(bitmapFirst, localFielName);//保存压缩好的图
}
public class BitmapCompressor {
   /**
    * @param reqWidth
    *            所需图片压缩尺寸最小宽度
    * @param reqHeight
    *            所需图片压缩尺寸最小高度
    * @return
    */
   public static Bitmap decodeSampledBitmapFromFile(String filepath, int reqWidth, int reqHeight) {
      BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();  
      bmpFactoryOptions.inSampleSize = 8;  
/*    Bitmap bmp = BitmapFactory.decodeFile(filepath, bmpFactoryOptions);  
      final BitmapFactory.Options options = new BitmapFactory.Options();
      *//**
       *  inJustDecodeBounds为true 不返回实际bitmap
       *//*
      options.inJustDecodeBounds = true;
      *//**
       * 解码图片路径为一个位图。如果指定的文件名是空的,或者不能解码到一个位图,函数将返回null[空值]。
       * 获取到outHeight(图片原始高度)和outWidth(图片的原始宽度)
       * 
       *//*
      BitmapFactory.decodeFile(filepath, options);
      *//**
       * 计算缩放比例
       *//*
      options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight)+2;
      @SuppressWarnings("unused")
      int a = options.inSampleSize ;
      *//**
       * 重新读出图片
       *//*
      options.inJustDecodeBounds = false;*/
      Bitmap bitmap = BitmapFactory.decodeFile(filepath, bmpFactoryOptions);
      return bitmap;
   }

   /*
    * bitmap转文件
    */
   public static boolean saveBitmap2file(Bitmap bmp, String filename) {
      File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "picss");
      if (!f.exists()) {
         f.mkdirs();
      }
      CompressFormat format = Bitmap.CompressFormat.JPEG;
      int quality = 100;
      OutputStream stream = null;
      try {
         stream = new FileOutputStream("/storage/sdcard0/pics/" + filename);//压缩图所在文件夹
      } catch (FileNotFoundException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      return bmp.compress(format, quality, stream);
   }
}

这样的话一般能缩小20%左右的样子

一半2M图片就是80到90K的样子

这是最简单的操作 在

decodeSampledBitmapFromFile

这个类中 可以做更多操作

作者水平有限 不喜勿喷

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值