Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid : Take a photo or an image from the gallery to crop it, runtimeException while changing Intent Uri
    primarykey
    data
    text
    <p>at some point in my application, I have to either take a photo or an image from the gallery and crop it. I manage to do all that, but the problem is that when I crop the image, it crops the original one, and I'd like to make a copy of it so I can reuse the original image.</p> <p>Here's my code :</p> <pre><code>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 &amp;&amp; (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 &amp;&amp; requestCode == CROP_PHOTO) { // method displaying the image from the Uri saveTakenPicture(imageFileUri); } } </code></pre> <p>And here are the errors I get when I try to use my file's uri :</p> <pre><code>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 </code></pre> <p>If anybody has a clue about what's wrong in my code, I'd be glad to hear it, because after 2 days of research I'm starting to desperate... Thanks a lot.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload