How to pass a URI to an intent?

115,474

Solution 1

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"));

Solution 2

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.

Solution 3

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();

Solution 4

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.

Solution 5

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"));
Share:
115,474

Related videos on Youtube

Robert El
Author by

Robert El

Updated on August 19, 2021

Comments

  • Robert El
    Robert El over 2 years

    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 ...
    
  • Robert El
    Robert El over 12 years
    ok cool ... but I don't know how to store a uri as a string :(
  • Caleb Jares
    Caleb Jares over 10 years
    Hint to anyone in the future: Make sure you're using android.net.Uri and not java.net.URI!
  • Armando
    Armando almost 10 years
    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.
  • Rufflez
    Rufflez about 9 years
    In the code above, its converted to a string. imageuri.toString() is converting the uri to a string for you.
  • clocksmith
    clocksmith about 8 years
    @malclocke has a better solution. No need to manually convert to string and back.
  • Buntupana
    Buntupana about 7 years
    You could use intent.setData(imageUri);
  • ban-geoengineering
    ban-geoengineering over 5 years
    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: stackoverflow.com/a/13981436/1617737 .
  • Brian
    Brian over 5 years
    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);
  • Pierre
    Pierre over 4 years
    Arrays works too! i.putExtra("URIList", uriList.toArray()); -> List<Uri> myList = i.getParcelableArrayListExtra("URIList");
  • Omar Boshra
    Omar Boshra over 4 years
    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