Reputation: 135
I have two Activities, A and B. I start B from a fragment of A, called F. I have a bitmap object in B. I need to pass the Uri of this bitmap to F. Here is sample code, This code is in F:
Intent bIntent = new Intent(getActivity(), B.class);
startActivityForResult(bIntent , 111);
This code is in B:
Intent aIntent = new Intent(B.this, A.class);
aIntent.putExtra("image", uri);
setResult(RESULT_OK, aIntent);
finish();
Again this code is in F:
if(requestCode == 111 && resultCode==Activity.RESULT_OK && data != null) {
Bundle extras = getActivity().getIntent().getExtras();
if (extras != null) {
Uri path = (Uri) extras.get("image");
ImageView iv = (ImageView) getActivity().findViewById(R.id.myImage);
iv.setImageURI(path);
}
}
But it doesn't work. I need the correct code in general and in this particular case.
Upvotes: 0
Views: 3203
Reputation: 1147
By default Android Uri class extends parcelable interface. You can take the inserted Uri with getParcelableExtra method like this:
Uri path = getActivity().getIntent().getParcelableExtra("image");
if(path != null) {
ImageView iv = (ImageView) getActivity().findViewById(R.id.myImage);
iv.setImageURI(path);
}
Or in your own way:
Bundle extras = getActivity().getIntent().getExtras();
if (extras != null) {
Uri path = (Uri) extras.getParcelable("image");
if(path != null) {
ImageView iv = (ImageView) getActivity().findViewById(R.id.myImage);
iv.setImageURI(path);
}
}
EDIT: Try to call iv.invalidate after you reset the image as such:
Uri path = result.getParcelableExtra("image");
if(path != null) {
ImageView iv = (ImageView) getActivity().findViewById(R.id.myImage);
iv.setImageURI(path);
iv.invalidate();
}
EDIT2: I just realise that there is a mistake in Activity B. Try to change and make sure you are using intent result which is parameter of onActivityResult instead of GetActivity
setResult(RESULT_OK, editIntent);
to
setResult(RESULT_OK, aIntent);
Upvotes: 3