120

I'm trying to pass a URI-Object to my Intent in order to use that URI in another activity.

How do I pass a URI?

private Uri imageUri;
....
Intent intent = new Intent(this, GoogleActivity.class);
intent.putExtra("imageUri", imageUri);
startActivity(intent);
this.finish();

How do I use now this URI in my other activity?

 imageUri = extras.getString("imageUri"); // I know thats wrong ...
İsmail Y.
  • 3,579
  • 5
  • 21
  • 29
Robert El
  • 1,247
  • 2
  • 10
  • 10
  • possible duplicate of [How to get extra data from intent in android?](http://stackoverflow.com/questions/4233873/how-to-get-extra-data-from-intent-in-android) – Matt Ball Nov 05 '11 at 01:03
  • possible duplicate of [How to send an object from one Android Activity to another using Intents?](http://stackoverflow.com/questions/2139134/how-to-send-an-object-from-one-android-activity-to-another-using-intents) – skynet Dec 04 '11 at 16:23

8 Answers8

205

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"));
Adi Krena
  • 3
  • 5
Lukap
  • 31,523
  • 64
  • 157
  • 244
  • 2
    ok cool ... but I don't know how to store a uri as a string :( – Robert El Nov 05 '11 at 01:12
  • In the code above, its converted to a string. imageuri.toString() is converting the uri to a string for you. – Rufflez Feb 18 '15 at 14:51
  • 1
    @malclocke has a better solution. No need to manually convert to string and back. – clocksmith Mar 04 '16 at 23:57
  • You could use intent.setData(imageUri); – Buntupana Mar 06 '17 at 13:42
  • 1
    For a moment I was wondering what "extras" meant. In simple terms: `intent.putExtra("imageUrl", mImageUri.toString());` **In other activity** `String imageUrl = getIntent().getStringExtra("imageUrl");` `Uri mImageUri = Uri.parse(imageUrl);` – Brian Jan 04 '19 at 13:55
151

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

// Add a Uri instance to an Intent
intent.putExtra("imageUri", uri);

// Get a Uri from an Intent
Uri 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.

malclocke
  • 5,192
  • 4
  • 20
  • 9
  • 12
    Hint to anyone in the future: Make sure you're using `android.net.Uri` and not `java.net.URI`! – Caleb Jares Jan 31 '14 at 19:32
  • 4
    For future reference, if you're putting several extras in a Bundle object before using `intent.putExtras(bundle);`, use `bundle.putParcelable("imageUri", uri);` instead of using `intent.putExtra(...);` directly in the `Intent` object. – Armando Jun 02 '14 at 00:21
  • Arrays works too! `i.putExtra("URIList", uriList.toArray());` -> `List myList = i.getParcelableArrayListExtra("URIList");` – Pierre Aug 20 '19 at 04:08
  • got this error when putting `java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Intent android.content.Intent.putExtra(java.lang.String, android.os.Parcelable)' on a null object reference` – Omar Boshra Dec 29 '19 at 02:35
64

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 Intent
Uri imageUri=getIntent().getData();
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
sandy
  • 3,311
  • 4
  • 36
  • 47
  • 2
    **Warning:** The above answer/solution should not be used for local broadcasts as you may find they won't get received. For local broadcasts, it's better to use malclocke's answer: https://stackoverflow.com/a/13981436/1617737 . – ban-geoengineering Aug 08 '18 at 12:32
1
private Uri imageUri;
....
Intent intent = new Intent(this, GoogleActivity.class);
intent.putExtra("imageUri", imageUri.toString());
startActivity(intent);
this.finish();


And then you can fetch it like this:

imageUri = Uri.parse(extras.getString("imageUri"));
silentw
  • 4,835
  • 4
  • 25
  • 45
1

here how I use it; This button inside my CameraActionActivity Activity class where I call camera

 btn_frag_camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intenImatToSec = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
                startActivityForResult(intenImatToSec, REQUEST_CODE_VIDEO);
                //intenImatToSec.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
                //intenImatToSec.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 10);
                //Toast.makeText(getActivity(), "Hello From Camera", Toast.LENGTH_SHORT).show();
            }
        });



@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {

            if (requestCode == REQUEST_CODE_IMG) {
                Bundle bundle = data.getExtras();
                Bitmap bitmap = (Bitmap) bundle.get("data");
                Intent intentBitMap = new Intent(getActivity(), DisplayImage.class);
                // aldıgımız imagi burda yonlendirdiğimiz sınıfa iletiyoruz
                ByteArrayOutputStream _bs = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.PNG, 50, _bs);
                intentBitMap.putExtra("byteArray", _bs.toByteArray());
                startActivity(intentBitMap);

            } else if (requestCode == REQUEST_CODE_VIDEO) {
                Uri videoUrl = data.getData();
                Intent intenToDisplayVideo = new Intent(getActivity(), DisplayVideo.class);
                intenToDisplayVideo.putExtra("videoUri", videoUrl.toString());
                startActivity(intenToDisplayVideo);
            }
        }
    }

And my other DisplayVideo Activity Class

VideoView videoView = (VideoView) findViewById(R.id.videoview_display_video_actvity);
Bundle extras = getIntent().getExtras();
        Uri myUri=  Uri.parse(extras.getString("videoUri"));
        videoView.setVideoURI(myUri);
Samir
  • 6,405
  • 5
  • 39
  • 42
1

If you want to use standard extra data field, you would do something like this:

private Uri imageUri;
....
Intent intent = new Intent(this, GoogleActivity.class);
intent.putExtra(Intent.EXTRA_STREAM, imageUri.toString());
startActivity(intent);
this.finish();

The documentation for Intent says:

EXTRA_STREAM   added in API level 1 
String EXTRA_STREAM
A content: URI holding a stream of data associated with the Intent,
used with ACTION_SEND to supply the data being sent. 

Constant Value: "android.intent.extra.STREAM"

You don't have to use the built-in standard names, but it's probably good practice and more reusable. Take a look at the developer documentation for a list of all the built-in standard extra data fields.

Tom Rutchik
  • 1,183
  • 1
  • 12
  • 15
0

The Uri.parse(extras.getString("imageUri")) was causing an error:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Intent android.content.Intent.putExtra(java.lang.String, android.os.Parcelable)' on a null object reference 

So I changed to the following:

intent.putExtra("imageUri", imageUri)

and

Uri uri = (Uri) getIntent().get("imageUri");

This solved the problem.

Wrichik Basu
  • 1,005
  • 17
  • 28
-3

you can do like this. imageuri can be converted into string like this.

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

Saad Bilal
  • 1,767
  • 1
  • 17
  • 31