在我的应用程序的某个时刻,我必须从画廊拍摄照片或图像并裁剪它。 我设法做到这一切,但问题是,当我裁剪图像时,它裁剪原始图像,我想复制它,以便我可以重复使用原始图像。
这是我的代码:
public void takePhoto(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, PICK_FROM_CAMERA);
}
public void browsePhotos(View view) {
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(intent, PICK_FROM_MEDIASTORE);
}
...
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && (requestCode == PICK_FROM_CAMERA || requestCode == PICK_FROM_MEDIASTORE)) {
// creating the file I want to modify
File dir = getDir("img", Context.MODE_PRIVATE);
File f = new File("croppedImage.jpg");
try {
FileOutputStream fos = new FileOutputStream(f);
Bitmap bitmap = MediaStore.Images.Media.getBitmap(
this.getContentResolver(), data.getData());
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
} catch (FileNotFoundException e) {
Log.e("FileNotFoundException", "Error while writing file in onActivityResult");
e.printStackTrace();
} catch (IOException e) {
Log.e("IOException", "Error while editing bitmap");
e.printStackTrace();
}
// returns lot of errors when I'm trying to do this
// imageFileUri = Uri.fromFile(f);
// works correctly but modify the original image
imageFileUri = data.getData();
Intent intent2 = new Intent("com.android.camera.action.CROP", imageFileUri);
intent2.putExtra("crop", "true");
intent2.putExtra("aspectX", 80);
intent2.putExtra("aspectY", 107);
intent2.putExtra("outputX", 640);
intent2.putExtra("outputY", 856);
intent2.putExtra("scale", true);
intent2.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
imageFileUri);
startActivityForResult(intent2, CROP_PHOTO);
}
else if (resultCode == RESULT_OK && requestCode == CROP_PHOTO) {
// method displaying the image from the Uri
saveTakenPicture(imageFileUri);
}
}
以下是我尝试使用文件的uri时出现的错误:
java.lang.RuntimeException: Unable to resume activity {[myPackage].[myActivity]}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { act=com.htc.HTCAlbum.action.ITEM_PICKER_FROM_COLLECTIONS dat=content://media/external/images/media/704 typ=image/jpeg (has extras) }} to activity {[myPackage].[myActivity]}: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.android.camera.action.CROP dat=file:///data/data/[myPackage]/app_img/croppedImage.jpg (has extras) }
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2434)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2455)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1842)
at android.app.ActivityThread.access$1500(ActivityThread.java:132)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:4196)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { act=com.htc.HTCAlbum.action.ITEM_PICKER_FROM_COLLECTIONS dat=content://media/external/images/media/704 typ=image/jpeg (has extras) }} to activity {[myPackage].[myActivity]}: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.android.camera.action.CROP dat=file:///data/data/[myPackage]/croppedImage.jpg (has extras) }
at android.app.ActivityThread.deliverResults(ActivityThread.java:2883)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2411)
... 12 more
Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.android.camera.action.CROP dat=file:///data/data/[myPackage]/croppedImage.jpg (has extras) }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1569)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1539)
at android.app.Activity.startActivityForResult(Activity.java:2901)
at [myPackage].[myActivity].onActivityResult([myActivity].java:539)
at android.app.Activity.dispatchActivityResult(Activity.java:4010)
at android.app.ActivityThread.deliverResults(ActivityThread.java:2879)
... 13 more
如果有人对我的代码中的错误有所了解,我会很高兴听到它,因为经过2天的研究我开始绝望了...... 非常感谢。