I have a class where i have navigation drawer. Then as a navigation drawer option I have a fragment,in which if we click on an option then another fragment is called replacing the current one. In the new fragment on clicking of a button I want to select an image from the gallery and then display it in the same fragment.
I have successfully got to the part where we select the image from the gallery,but after selecting the image, the app goes to the calling activity.
I am using startActivityForResult and onActivityResult for this.
private void selectImage() {
final CharSequence[] items = { "Take Photo", "Choose from Library", "Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
builder.setTitle("Add Photo!");
builder.setItems(items, new DialogInterface.OnClickListener() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Take Photo")) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
} else if (items[item].equals("Choose from Library")) {
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(
Intent.createChooser(intent, "Select File"),
SELECT_FILE);
} else if (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_CAMERA) {
Toast.makeText(this.getActivity().getBaseContext(), "Reached onActivityResult:Camera", Toast.LENGTH_LONG).show();
} else if (requestCode == SELECT_FILE) {
Toast.makeText(this.getActivity().getBaseContext(), "Reached onActivityResult:Gallery", Toast.LENGTH_LONG).show();
}
}
}
After selecting an option from the dialog and then selecting the image,onActivityResult is getting called, but then the hosting activity of the fragment is called and the view changes and the image does not persist
Any help is appreciated.
TIA