android 7.0 相机,拍照 调裁切提示 “无法加载此图片” 解决方案

 

调起相机拍照之后,在调起裁切之前  加上 这两句代码,,

Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
//需要加上这两句话 : uri 权限
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

intent.putExtra("crop", "true");
intent.putExtra("aspectX", 400);
intent.putExtra("aspectY", 400);
intent.putExtra("output", Uri.fromFile(loadingFile));// 输出到文件
intent.putExtra("outputFormat", "PNG");// 返回格式
intent.putExtra("noFaceDetection", true); // 去除面部检测
intent.putExtra("return-data", false); // 不要通过Intent传递截获的图片
startActivityForResult(intent, AVATAR_ZOOM);



方法代码:路径 文件夹的  内容有删减,,谢谢,

private void choosePicture() {
final Dialog dialog = new Dialog(getActivity(), R.style.dialog);
View contentView = getActivity().getLayoutInflater().inflate(R.layout.user_photo_dialog, null);
dialog.setContentView(contentView);

android.view.ViewGroup.LayoutParams cursorParams = contentView.getLayoutParams();
cursorParams.width = getResources().getDisplayMetrics().widthPixels;
contentView.setLayoutParams(cursorParams);
dialog.getWindow().setWindowAnimations(R.style.dialogWindowAnim); // 设置窗口弹出动画
dialog.show();

TextView album = (TextView) dialog.findViewById(R.id.avatar_album);
TextView photograph = (TextView) dialog.findViewById(R.id.avatar_photograph);
TextView cancel = (TextView) dialog.findViewById(R.id.avatar_cancel);

//从相册选择上传
album.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
dialog.dismiss();
Intent picture = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(picture, AVATAR_ALBUM);
}
});

//拍照上传
photograph.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
if (mPhotographFile == null) {
mPhotographFile = new File(AppStoragePath.getCachePath() + "/img/");
if (!mPhotographFile.exists()) {
mPhotographFile.mkdirs();
}
}
mPhotographPath = mPhotographFile + "/" + UserAppConst.imageName();
mEditor.putString(UserAppConst.PHOTO_PATH, mPhotographPath);
mEditor.commit();


File file = new File(mPhotographPath);
// Uri imageuri = Uri.fromFile(file);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, getUriForFile(getActivity(), file));
intent.putExtra("return-data", false);
startActivityForResult(intent, AVATAR_PHOTOGRAPH);
}
});
//取消
cancel.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (resultCode == Activity.RESULT_OK) {
if (requestCode == AVATAR_PHOTOGRAPH) {
mPhotographPath = mShared.getString(UserAppConst.PHOTO_PATH, null);
if (mPhotographPath != null) {
File files = new File(mPhotographPath);
if (files.exists()) {
mImagePath = mPhotographPath;
mImagePath = startPhotoZoom(getUriForFile(getActivity(), files));
}
}
} else if (requestCode == AVATAR_ALBUM) {
Uri selectedImage = data.getData();
mImagePath = startPhotoZoom(selectedImage);
} else if (requestCode == AVATAR_ZOOM) {
if (mImagePath != null && mImagePath.length() > 0) {
mImagePath = ImageUtil.zoomImage(mImagePath, 400);
Bitmap bitmap = BitmapFactory.decodeFile(mImagePath);
mPhoto.setImageBitmap(ImageLoaderUtils.getInstance().toRoundBitmap(bitmap));
ToastUtil.toastShow(getActivity(), getString(R.string.modify_avatar_succeed));

}
}
}
}

private String startPhotoZoom(Uri uri) {

try {



File loadingFile = new File(filePath);
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 400);
intent.putExtra("aspectY", 400);
intent.putExtra("output", Uri.fromFile(loadingFile));// 输出到文件
intent.putExtra("outputFormat", "PNG");// 返回格式
intent.putExtra("noFaceDetection", true); // 去除面部检测
intent.putExtra("return-data", false); // 不要通过Intent传递截获的图片
startActivityForResult(intent, AVATAR_ZOOM);
return filePath;
} catch (Exception e) {
Log.i("SNN", "startPhotoZoom: e " + e.toString());
}

return null;

}

public static Uri getUriForFile(Context context, File file) {
if (context == null || file == null) {
throw new NullPointerException();
}
Uri uri;
if (Build.VERSION.SDK_INT >= 24) {
uri = FileProvider.getUriForFile(context.getApplicationContext(), "

(你的packagename +).fileprovider", file);

} else { uri = Uri.fromFile(file); } return uri;}

 

你可能感兴趣的:(Android,7.0相册,拍照,7.0相机相册适配,7.0无法加载此图片)