Android beginner here..
I have a method in my Dialog Fragment to take a photo:
public void takePhoto() {
Intent takePhoto = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
getActivity().startActivityForResult(takePhoto, REQUEST_TAKE_PHOTO);
}
And I realize that the onActivityResult method has to be in the "fragment calling activity", but how would I set an ImageView in the Dialog from the onActivityResult?
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
// Set an ImageView in my Dialog Fragment
mImageView.setImageBitmap(imageBitmap);
}
}
SOLUTION:
Declared this field in my DialogFragment:
private static View layout;
...
layout = View.inflate(mContext, R.layout.new_post_dialog_layout, null);
Added this method to my DialogFragment:
public static ImageView getDialogImageView() {
ImageView iv = (ImageView) layout.findViewById(R.id.imagePost);
return iv;
}
Modified the onActivityResult:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
NewPostDialog.getDialogImageView().setImageBitmap(imageBitmap);
}
}
I would think you could just create a public method in your dialog that gives access to the image view in its custom view.
So like public ImageView getDialogImageView()
, then in your activity just check if the Image view is null and if not then set the image in onActivityResult
. You might also want to use a WeakReference to do this so it can be garbage collected when you're done.