Когда я обрезаю фотографию, использую внутренний компонент обрезки системы , он работает хорошо. Но когда я выбираю приложение третьей части SnapSeed, когда я нажимаю кнопку сохранения, держу тост «Не удалось сохранить фотографию!». Затем я открываю файл создан, его размер составляет 0 КБ, эта проблема действительно странная.
Как я могу решить эту проблему?
Вот мой код:
public void corpImg(Uri inputUri) {
mCorpImg = null;
try {
mCorpImg = AndroidFileUtils.createTempImageFile(getContext());
while (!mCorpImg.exists()) {
Log.e("Failed!");
mCorpImg.createNewFile();
}
} catch (IOException e) {
e.printStackTrace();
return;
}
mCorpUri = Uri.fromFile(mCorpImg);
try {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(inputUri, "image/*");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 640);
intent.putExtra("outputY", 640);
intent.putExtra("return-data", false);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mCorpUri);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
grantUriPermission(intent, mCorpUri);
startActivityForResult(intent, REQUEST_CORP);
} catch (Exception e) {
e.printStackTrace();
HKToast.normal("Failed!").show();
}
}
private void grantUriPermission(Intent intent, Uri photoURI) {
List<ResolveInfo> resolveInfos = mActivity.getPackageManager()
.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resolveInfos) {
mActivity.grantUriPermission(resolveInfo.activityInfo.packageName, photoURI,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
}
/**
* Creates the temporary image file in the cache directory.
*
* @return The temporary image file.
* @throws IOException Thrown if there is an error creating the file
*/
public static File createTempImageFile(Context context) throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
return getTempImg(context, imageFileName);
}
@NonNull
public static File getTempImg(Context context, String imageFileName) {
File storageDir = new File(getCacheDir(context), "temp_img");
if (storageDir.exists()) {
return new File(storageDir, imageFileName + ".jpg");
} else if (storageDir.mkdir()) {
return new File(storageDir, imageFileName + ".jpg");
} else {
return new File(getCacheDir(context), imageFileName + ".jpg");
}
}
public static File getCacheDir(Context context) {
File cacheDir;
if (isExternalStorageReadable() && isExternalStorageWritable()) {
cacheDir = context.getExternalCacheDir();
} else {
cacheDir = context.getCacheDir();
}
if (cacheDir != null && cacheDir.exists()) {
return cacheDir;
} else {
HKLog.e("CacheDir not created");
return null;
}
}