解决: android11 系统裁剪图片
作者:互联网
FileProvider.getUriForFile 请自行查阅资料
public void startPhotoZoom(Uri uri){
if(uri == null)
{
finish();
return;
}
String name = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()) +"cut.jpg";
File CropPhoto = new File(getExternalCacheDir(), name);
//m_ResultImagePath = CropPhoto.getPath();
mPhotoUri = Uri.fromFile(CropPhoto);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
{
CropPhoto = uriToFileApiQ(uri, context);
mPhotoUri = FileProvider.getUriForFile(context,
getPackageName() + ".fileprovider", CropPhoto);
}
headPath = CropPhoto.getPath();
Intent intent = new Intent("com.android.camera.action.CROP");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
{
intent.setDataAndType(mPhotoUri, "image/*");
}
else
{
intent.setDataAndType(uri, "image/*");
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION ); //添加这一句表示对目标应用临时授权该Uri所代表的文件
}
intent.putExtra("crop", "true");
intent.putExtra("scale", true);
if(Build.MANUFACTURER.equals("HUAWEI")) {
intent.putExtra("aspectX", 9998);
intent.putExtra("aspectY", 9999);
}
else {
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
}
intent.putExtra("outputX", headWidth);
intent.putExtra("outputY", headHight);
intent.putExtra("return-data", false);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mPhotoUri);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
//intent.putExtra("noFaceDetection", true); // no face detection
try {
startActivityForResult(intent, RESULT_REQUEST_CODE);//这里的RESULT_REQUEST_CODE是在startActivityForResult里使用的返回值。
} catch (Exception e) {
// 解决截取后部分机型秒退
e.printStackTrace();
}
}
uriToFileApiQ函数:
@TargetApi(Build.VERSION_CODES.Q)
public static File uriToFileApiQ(Uri uri, Context context) {
File file = null;
if(uri == null) return file;
//android10以上转换
if (uri.getScheme().equals(ContentResolver.SCHEME_FILE)) {
file = new File(uri.getPath());
} else if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
//把文件复制到沙盒目录
ContentResolver contentResolver = context.getContentResolver();
String displayName = System.currentTimeMillis()+ Math.round((Math.random() + 1) * 1000)
+"."+ MimeTypeMap.getSingleton().getExtensionFromMimeType(contentResolver.getType(uri));
// 注释掉的方法可以获取到原文件的文件名,但是比较耗时
// Cursor cursor = contentResolver.query(uri, null, null, null, null);
// if (cursor.moveToFirst()) {
// String displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));}
try {
InputStream is = contentResolver.openInputStream(uri);
File cache = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath(), displayName);
FileOutputStream fos = new FileOutputStream(cache);
FileUtils.copy(is, fos);
file = cache;
fos.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return file;
}
标签:putExtra,android11,裁剪,uri,VERSION,intent,Build,new,图片 来源: https://blog.csdn.net/xiangyubimu1/article/details/115127354