Jonah G
Jonah G

Reputation: 81

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

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?

Upvotes: 1

Views: 13234

Answers (5)

ali sampson
ali sampson

Reputation: 467

In your first class, you can pass the image uri like this:

Intent intent = new Intent();
intent.putExtra("your_key", imageUri.toString());
startActivity(intent);

And in the second or receiver activity, you can access the image uri this way:

Bundle extras = getIntent().getExtras();
if(extras != null){
  Uri imageUri = Uri.parse(extras.getString("your_key"));
}

Upvotes: 0

Sunil
Sunil

Reputation: 3793

  1. First Activity

     Uri uri = data.getData();
            Intent intent=new Intent(Firstclass.class,secondclass.class);
            intent.putExtra("imageUri", uri.toString());
            startActivity(intent);
    
  2. Second class

    Imageview iv_photo=(ImageView)findViewById(R.id.iv_photo);
    Bundle extras = getIntent().getExtras();
    myUri = Uri.parse(extras.getString("imageUri"));
    iv_photo.setImageURI(myUri);
    

Upvotes: 0

Adam
Adam

Reputation: 51

        //First Activity to get a Uri
        String uri_Str = Uri.toString();

        Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
        intent .putExtra("uri_Str", uri_Str);
        startActivity(intent);


       //Second Activity get a Uri path
        Bundle b = getIntent().getExtras();
        if (b != null) {
        String uri_Str= b.getString("uri_Str");
        Uri uri = Uri.parse(uri_Str);
        }

Upvotes: 0

Saurabh Rajpal
Saurabh Rajpal

Reputation: 1557

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.

Upvotes: 3

Miki Franko
Miki Franko

Reputation: 687

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

Upvotes: 4

Related Questions