How to pass a URI to an intent? How to pass a URI to an intent? android android

How to pass a URI to an intent?


you can store the uri as string

intent.putExtra("imageUri", imageUri.toString());

and then just convert the string back to uri like this

Uri myUri = Uri.parse(extras.getString("imageUri"));


The Uri class implements Parcelable, so you can add and extract it directly from the Intent

// Add a Uri instance to an Intentintent.putExtra("imageUri", uri);// Get a Uri from an IntentUri uri = intent.getParcelableExtra("imageUri");

You can use the same method for any objects that implement Parcelable, and you can implement Parcelable on your own objects if required.


In Intent, you can directly put Uri. You don't need to convert the Uri to string and convert back again to Uri.

Look at this simple approach.

// put uri to intent intent.setData(imageUri);

And to get Uri back from intent:

// Get Uri from IntentUri imageUri=getIntent().getData();