Android 照片压缩
16lz
2021-01-23
今天给大家带来的是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);//手机内存的picsif (!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);//应用启动默认的第一个启动的activityintent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);//将照片存储两次,分别是系统和自己创建的startActivityForResult(intent, 0);
注释写的很清楚
@Overrideprotected 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
这个类中 可以做更多操作
作者水平有限 不喜勿喷
更多相关文章
- Android 上传图片到服务器(多文件上传)
- android zxing 解析二维码图片与生成二维码图片
- Android之glide加载图片圆角效果
- Android多点触控(图片的缩放Demo)
- android 层叠图片形成一张图片
- Android中的ListView实现图片文字和按钮
- android 在SDCard获取图片
- android 从系统相册获取一张图片
- android WebView加载URL不显示图片