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?
Asked
Active
Viewed 1.3k times
1
-
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. – nano_nano Feb 05 '15 at 16:12
-
Can you please make an answer with sample code. I'll be happy to accept it if it works. – Jonah G Feb 05 '15 at 16:14
-
Don't send an entire bitmap via intent; send the uri itself as an extra – Eenvincible Feb 05 '15 at 16:16
5 Answers
4
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);

Miki Franko
- 687
- 3
- 7
-
-
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 Feb 05 '15 at 16:27
-
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. https://docs.google.com/document/d/1y0yG_4vLrLzRww3C5_fNasUHQwAoKazIS6jEKC-sqGE/pub https://docs.google.com/document/d/17620g9p2wZ4qEIrYWhO3ilOkgKn2rm8p68FcDdu6R7Q/pub – Jonah G Feb 07 '15 at 22:12
3
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.

Saurabh Rajpal
- 1,537
- 9
- 14
0
//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);
}

Adam
- 51
- 4
0
First Activity
Uri uri = data.getData(); Intent intent=new Intent(Firstclass.class,secondclass.class); intent.putExtra("imageUri", uri.toString()); startActivity(intent);
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);

Sunil
- 3,785
- 1
- 32
- 43
0
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"));
}

Dharman
- 30,962
- 25
- 85
- 135

ali sampson
- 321
- 4
- 7