Reputation: 81
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
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
Reputation: 3793
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);
Upvotes: 0
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
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
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