1.今天总结了一下安卓调用系统相机拍照,并把图片保存到SD卡中 进行 尺寸和质量的压缩的问题,顺便整理了一下自己的思路。
解决办法:
(1)调取系统相机,并指定url路径
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(new File(getPhotopath()))); startActivityForResult(intent, 1);
/** * 获取原图片存储路径 * * @return */ private String getPhotopath() { // 照片全路径 // 文件夹路径 String pathUrl = Environment.getExternalStorageDirectory() + "/mymyss/"; // String imageName = "imageTest.jpg"; File file = new File(pathUrl); if (!file.exists()) { file.mkdirs(); } fileName = pathUrl + System.currentTimeMillis() + ".jpg"; // fileName = pathUrl + imageName; Log.d("fileName",fileName+"---"); return fileName; }
static String fileName = "";//为什么要定义成静态的 因为有些手机在拍照后 acvitity会重新创建 oncreate方法(2)接收处理拍照结果
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { Log.d("resultCode",resultCode+","+requestCode); if(requestCode==1) { Log.d("fileName",fileName+"-12--"); if (!fileName.equals("")) { getimage(fileName); Toast.makeText(getApplicationContext(),"图片压缩成功",Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getApplicationContext(),"图片空",Toast.LENGTH_SHORT).show(); } } }(3)先进行尺寸压缩 ,在进行质量压缩
/** * 图片按比例大小压缩方法 * * @param srcPath (根据路径获取图片并压缩) * @return */ public Bitmap getimage(String srcPath) { BitmapFactory.Options newOpts = new BitmapFactory.Options(); // 开始读入图片,此时把options.inJustDecodeBounds 设回true了 newOpts.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);// 此时返回bm为空 newOpts.inJustDecodeBounds = false; int w = newOpts.outWidth; int h = newOpts.outHeight; // 现在主流手机比较多是800*480分辨率,所以高和宽我们设置为 float hh = this.h;// 这里设置高度为800f float ww = this.w;// 这里设置宽度为480f // 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可 int be = 1;// be=1表示不缩放 if (w > h && w > ww) {// 如果宽度大的话根据宽度固定大小缩放 be = (int) (newOpts.outWidth / ww); // be=Math.round((float) w / (float) ww); } else if (w < h && h > hh) {// 如果高度高的话根据宽度固定大小缩放 be = (int) (newOpts.outHeight / hh); // be=Math.round((float) h / (float) hh); } if (be <= 0) be = 1; newOpts.inSampleSize = be;// 设置缩放比例 // 重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了 bitmap = BitmapFactory.decodeFile(srcPath, newOpts); return compressImage(bitmap,fileName);// 压缩好比例大小后再进行质量压缩 }/** * 质量压缩方法 * * @param srcPath (根据路径获取图片并压缩) * @return */public Bitmap compressImage(Bitmap bitmap,String path) { // File file = null; // Bitmap bitmap = BitmapFactory.decodeFile(path);//读取路径下的图片 ByteArrayOutputStream baos = new ByteArrayOutputStream();//获取图像全质量的输出流 bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); int options = 90; while (baos.toByteArray().length/1024>400)//如果质量大于400kB 就不断的压缩 { baos.reset(); // 重置baos即清空baos bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);// 这里压缩options%,把压缩后的数据存放到baos中 options -= 10;// 每次都减少10 } try { file = new File(path);//文件形式获取图片 FileOutputStream out = new FileOutputStream(file);//文件输入流 // if (bitmap.compress(Bitmap.CompressFormat.JPEG, options, out)){//把数据写入文件的质量是百分之10 out.write(baos.toByteArray()); out.flush();//清空缓存区 out.close();//关闭流 // } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return bitmap; }调用以上方法后,在看下SD卡中的图片 是不是尺寸和质量都被压缩了