首先分析原因:裁剪时 return-data的方式只适用于小图,小米miui系统和华为系统默认的裁剪图片可能裁剪得过大,或对return-data分配的资源不足,造成return-data失败。
解决方法:裁剪后,将裁剪的图片保存在Uri中,在onActivityResult()方法中,再提取对应的Uri图片转换为Bitmap使用。
//拍照获取图片 private void takePhoto() { Intent intentFromCapture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // 判断存储卡是否可用,存储照片文件 if (hasSdcard()) {ContentValues values = new ContentValues(); values.put(MediaStore.Images.Media.TITLE, IMAGE_FILE_NAME); intentFromCapture.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), IMAGE_FILE_NAME))); }startActivityForResult(intentFromCapture, CODE_CAMERA_REQUEST); }
//从相册选取图片 private void selectPhotoFromGallery() {Intent intentFromGallery = new Intent(); // 设置文件类型 intentFromGallery.setType("image/*"); intentFromGallery.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(intentFromGallery, CODE_GALLERY_REQUEST);
/** * 裁剪原始的图片 */ public void cropRawPhoto(Uri uri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); // 设置裁剪 intent.putExtra("crop", "true"); // aspectX , aspectY :宽高的比例 intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); // outputX , outputY : 裁剪图片宽高 intent.putExtra("outputX", output_X); intent.putExtra("outputY", output_Y); // intent.putExtra("return-data", true); //原本的裁剪方式 //uritempFile为Uri类变量,实例化uritempFile,转化为uri方式解决问题 mUritempFile = Uri.parse("file://" + "/" + Environment.getExternalStorageDirectory().getPath() + "/" + "small.jpg"); intent.putExtra(MediaStore.EXTRA_OUTPUT, mUritempFile); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); startActivityForResult(intent, CODE_RESULT_REQUEST); }
@Override public void onActivityResult(int requestCode, int resultCode, Intent intent) {super.onActivityResult(requestCode, resultCode, intent); // 用户没有进行有效的设置操作,返回 if (resultCode == getActivity().RESULT_CANCELED) {return; }switch (requestCode) {case CODE_GALLERY_REQUEST: //相册 String replace = intent.getData().toString().replace("content", "file"); cropRawPhoto(Uri.parse(replace)); break; case CODE_CAMERA_REQUEST: //相机 if (hasSdcard()) { File tempFile = new File(Environment.getExternalStorageDirectory(), IMAGE_FILE_NAME); Log.e("ffl", "onActivityResult: path==== " + tempFile.getAbsolutePath()); cropRawPhoto(Uri.fromFile(tempFile)); } else {Toast.makeText(getActivity(), "没有SDCard!", Toast.LENGTH_LONG).show(); }break; case CODE_RESULT_REQUEST: if (intent != null) { //将Uri图片转换为Bitmap try {Bitmap bitmap = BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(mUritempFile)); //TODO,将裁剪的bitmap显示在imageview控件上 setImageToHeadView(bitmap); } catch (FileNotFoundException e) {e.printStackTrace(); } // setImageToHeadView(intent); }else {Log.e("ffl", "onActivityResult: -------------intent为null------------"); }break; }}
但是小米手机选择相册时,如果不使用系统截屏,可以使用,但是如果截屏就会报“保存时发生错误,保存失败”;
这时我们需要做一个转换,把URI地址转为图片地址,再包成file文件转为URI,就可以使用了,代码如下:
- Uri uri = intent.getData();
- String type = intent.getType();
- if (uri.toString().contains("com.miui.gallery.open")) {
- uri = getImageContentUri(activity, new File(getRealFilePath(activity, uri)));
- }
- /**
- * 将URI转为图片的路径
- *
- * @param context
- * @param uri
- * @return
- */
- public static String getRealFilePath(final Context context, final Uri uri) {
- if (null == uri)
- return null;
- final String scheme = uri.getScheme();
- String data = null;
- if (scheme == null)
- data = uri.getPath();
- else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
- data = uri.getPath();
- } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
- Cursor cursor = context.getContentResolver().query(uri,
- new String[]{ImageColumns.DATA}, null, null, null);
- if (null != cursor) {
- if (cursor.moveToFirst()) {
- int index = cursor.getColumnIndex(ImageColumns.DATA);
- if (index > -1) {
- data = cursor.getString(index);
- }
- }
- cursor.close();
- }
- }
- return data;
- }
-
- /**
- *
- * 包成file文件转为URI
- *
- */
- public static Uri getImageContentUri(Context context, File imageFile) {
- String filePath = imageFile.getAbsolutePath();
- Cursor cursor = context.getContentResolver().query(
- MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
- new String[]{MediaStore.Images.Media._ID},
- MediaStore.Images.Media.DATA + "=? ",
- new String[]{filePath}, null);
- if (cursor != null && cursor.moveToFirst()) {
- int id = cursor.getInt(cursor
- .getColumnIndex(MediaStore.MediaColumns._ID));
- Uri baseUri = Uri.parse("content://media/external/images/media");
- return Uri.withAppendedPath(baseUri, "" + id);
- } else {
- if (imageFile.exists()) {
- ContentValues values = new ContentValues();
- values.put(MediaStore.Images.Media.DATA, filePath);
- return context.getContentResolver().insert(
- MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
- } else {
- return null;
- }
- }
- }