Search code examples
javaandroidonactivityresult

Expected Parcelable for result for camera Intent


I am making a camera intent and storing the snapshot using the activity result, this is my code:

File imageFolder=new File(context.getExternalCacheDir(),"Cam/" + form);
imageFolder.mkdirs();
String random= UUID.randomUUID().toString();
String filename = random + ".jpg";
TakenImage =  imageFolder + "/" + filename;
Intent camera=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
camera.putExtra(MediaStore.EXTRA_OUTPUT,TakenImage);
activityResultCamera.launch(camera);

But I get this error on the last line:

Key output expected Parcelable but value was a java.lang.String. The default value was returned.

Attempt to cast generated internal exception:

java.lang.ClassCastException: java.lang.String cannot be cast to android.os.Parcelable

Camera is an Intent, I also declared ActivityResultLauncher as Intent

ActivityResultLauncher<Intent> activityResultCamera = registerForActivityResult(...

So, what I am doing wrong?


Solution

  • EXTRA_OUTPUT needs to be:

    • A Uri...
    • With a scheme of content

    You have:

    • A String...
    • That is a filesystem path that other apps cannot access on Android 10+

    Use FileProvider to get a Uri that points to your desired location, and use that Uri in EXTRA_OUTPUT.