How to transfer a Uri image from one activity to another?

12,171

Solution 1

use with putExtra to send the Uri Path:

            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent .setClass(ThisActivity.this,  NewActivity.class);
            intent .putExtra("KEY", Uri);
            startActivity(intent );

In the newActivity OnCreate method:

   Bundle extras = getIntent().getExtras();
    if (extras != null && extras.containsKey("KEY")) {
        Uri= extras.getString("KEY");
    }

Use those func: Uri to String:

Uri uri;
String stringUri;
stringUri = uri.toString();

String to Uri:

Uri uri;
String stringUri;
uri = Uri.parse(stringUri);

Solution 2

To avoid the error you are getting, in the code given by Miki franko, replace the line :

Uri= extras.getString("KEY");

with :

uri= Uri.parse(extras.getString("KEY"));

This is just to make the code work as I think you didn't understand what Miki tried to explain through the code.
Keep us posted if you get it resolved now.

Share:
12,171
Jonah G
Author by

Jonah G

Updated on November 27, 2022

Comments

  • Jonah G
    Jonah G over 1 year

    In my app I need to transfer a Uri image from my first Activity to another. I know how to send a Bitmap through an intent. I'm a bigginer programmer so I don't know what would be better to do: transfer the Uri with an intent or change the Uri to a Bitmap then send that?

    • s_bei
      s_bei about 9 years
      if it is only one process just add a public static field to your activity class and assign the Uri to it before you execute the intent.
    • Jonah G
      Jonah G about 9 years
      Can you please make an answer with sample code. I'll be happy to accept it if it works.
    • Eenvincible
      Eenvincible about 9 years
      Don't send an entire bitmap via intent; send the uri itself as an extra
  • Jonah G
    Jonah G about 9 years
    How do I access the Uri in the second Activity?
  • Jonah G
    Jonah G about 9 years
    I placed the buttom code in the second Activity in a Uri and I got an error that I should place it in a String
  • Jonah G
    Jonah G about 9 years
    Thank you very much! In general the code works but the transition in the way I want to use it doesn't. I want to take a photo from the gallery then put it over a camera preview on a different Activity. The app crashes when I transfer to the camera preview page and I really don't know why... I attached links to the activity for picking a photo fron the gallery and the camera priview activity, please help me if you can. docs.google.com/document/d/… docs.google.com/document/d/…