标签:
首先是打开本地相册选取图片:
Intent albumIntent = new Intent(Intent.ACTION_PICK);//打开系统的相册 albumIntent.setType("image/*"); startActivityForResult(albumIntent, 0x1004);
选取完图片后在onActivityResult编写取数据的操作:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (data==null) { if (isNull) { }else { if (requestCode==0x1004) {//从相册选取图片 getClipPhotoByPickPicture(data.getData(),data.getExtras()); } } }获得返回数据对图片进行裁剪:
/** * 取得返回的照片信息 * @param uri 传进返回数据的uri * @param bundle 传进返回数据的bundle */ private void getClipPhotoByPickPicture(Uri uri, Bundle bundle) { if (uri == null) { /** * */ } else { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1);// aspectX是宽高的比例,这里设置的是正方形(长宽比为1:1) intent.putExtra("aspectY", 1); intent.putExtra("outputX", 400); // outputX outputY 是裁剪图片宽高 intent.putExtra("outputY", 400); //不知怎么了,我设置不能设太大,<640 intent.putExtra("scale", true); intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); intent.putExtra("return-data", true); intent.putExtra("outputFormat",Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); startActivityForResult(intent, 0x1006); } }
boolean isNull=false;//判断照相机返回数据是否为空 Intent getImageByCamera = new Intent("android.media.action.IMAGE_CAPTURE"); File file = new File(Environment.getExternalStorageDirectory() + "/Elephant/accountImg/accountImg.jpg"); if (file.exists()) { file.delete(); } //指定uri存储相片 getImageByCamera.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(file)); isNull = true; startActivityForResult(getImageByCamera, 0x1005);
if (data==null) { if (isNull) {//当返回的data为空,并且isNull为true,说明相机所拍的照片已保存 File fileImg = new File(Environment.getExternalStorageDirectory() + "/Elephant/accountImg/accountImg.jpg"); Uri uriFromImg=Uri.fromFile(fileImg);//提取uri //裁剪图片 Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uriFromImg, "image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1);// aspectX是宽高的比例,这里设置的是正方形(长宽比为1:1) intent.putExtra("aspectY", 1); intent.putExtra("outputX", 400); // outputX outputY 是裁剪图片宽高 intent.putExtra("outputY", 400); //不知怎么了,我设置不能设太大,<640 intent.putExtra("scale", true); intent.putExtra(MediaStore.EXTRA_OUTPUT, uriFromImg); intent.putExtra("return-data", true); intent.putExtra("outputFormat",Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); // no face detection startActivityForResult(intent, 0x1006); Log.d("logd", "uri后面"); isNull=false; } }else { //当data!=null }
同样在onActivityResult中编写,让裁剪后的图片显示在imageView上:
if (requestCode == 0x1006) { //接收裁剪好的图片信息并保存到本地文件夹 Bitmap bmap = data.getParcelableExtra("data"); iv_show_img.setImageBitmap(bmap); // 图像保存到文件中 FileOutputStream foutput = null; try { File fileDir = new File( Environment.getExternalStorageDirectory() + "/Elephant/accountImg/"); if (!fileDir.exists()) { fileDir.mkdirs(); } File fileImg = new File(fileDir, "accountImg.jpg"); if (fileImg.exists()) { fileImg.delete(); } foutput = new FileOutputStream(fileImg); bmap.compress(Bitmap.CompressFormat.JPEG, 50, foutput); // 压缩图片 } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (null != foutput) { try { foutput.close(); } catch (IOException e) { e.printStackTrace(); } } } }
参考:
http://blog.csdn.net/beyond0525/article/details/8939984
http://blog.csdn.net/it_transformers/article/details/42060775#comments
标签:
原文地址:http://blog.csdn.net/an_illusion/article/details/51347615