80,361
社区成员
发帖
与我相关
我的任务
分享
@SuppressWarnings("all")
public static Intent cropPicture(Context context, Uri source, int aspectX, int aspectY, int outX, int outY, boolean scaled, boolean returnData, boolean circleCrop) {
String author = context.getPackageName() + AUTHOR;
if (ContentResolver.SCHEME_FILE.equals(source.getScheme())) {
//android N 不允许向外部应用发送file://的uri
File file = new File(source.getPath());
if (file.exists()) {
try {
source = FileProvider.getUriForFile(context, author, file);
} catch (Exception e) {
//如果返回的是未配置的路径则使用原始的uri
Timber.e(e);
}
}
}
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(source, "image/*");
intent.putExtra("crop", true);
intent.putExtra("aspectX", aspectX);
intent.putExtra("aspectY", aspectY);
intent.putExtra("outputX", outX);
intent.putExtra("outputY", outY);
intent.putExtra("scale", scaled);
intent.putExtra("return-data", returnData);
intent.putExtra("outputFormat", Bitmap.CompressFormat.PNG);
intent.putExtra("circleCrop", circleCrop);
File cropTempFile = new File(StorageUtils.getProperCacheDir(context, TEMP_DIRECTORY), CROP_TEMP_FILE);
if (!cropTempFile.getParentFile().exists()) {
cropTempFile.getParentFile().mkdirs();
}
if (cropTempFile.exists()) {
cropTempFile.delete();
}
Uri outPutUri = FileProvider.getUriForFile(context, author, cropTempFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outPutUri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
// if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
try {
List<ResolveInfo> resInfoList = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
context.grantUriPermission(packageName, outPutUri, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
context.grantUriPermission(packageName, source, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
} catch (SecurityException e) {
//maybe no permission to grant origin source uri
//if picture is large then this operation will block main thread long time
InputStream in = null;
FileOutputStream outputStream = null;
try{
in = context.getContentResolver().openInputStream(source);
File cropTempSourceFile = new File(StorageUtils.getProperCacheDir(context,TEMP_DIRECTORY),CROP_TEMP_SOURCE_FILE);
if (!cropTempSourceFile.getParentFile().exists()) {
cropTempSourceFile.getParentFile().mkdirs();
}
outputStream = new FileOutputStream(cropTempSourceFile);
byte[] buffer = new byte[512];
int len;
while ((len = in.read(buffer)) != -1){
outputStream.write(buffer,0,len);
}
outputStream.flush();
outputStream.close();
outputStream = null;
in = null;
Uri newSource = FileProvider.getUriForFile(context,author,cropTempSourceFile);
intent.setDataAndType(newSource, "image/*");
}catch (Exception e1){
Timber.e(e1);
}finally {
CloseUtils.closeIOQuietly(in,outputStream);
}
}catch (Exception e){
Timber.d(e);
}
// }
return intent;
}