I have two ImageViews and two buttons. When the button is pressed, a dialogue has begun. The positive answer is the image from the gallery, the negative - from the camera. First I write this in Activity (this code works well):
@Override protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { String path = null; if (DialogFlag == 0) { super.onActivityResult(requestCode, resultCode, imageReturnedIntent); switch (requestCode) { case GALLERY_REQUEST: if (resultCode == RESULT_OK) { Uri selectedImage = imageReturnedIntent.getData(); path = getRealPathFromURI(selectedImage); Log.d("myLogs", path); if (btnID == 1) { pathOne = path; Bitmap bmImg = BitmapFactory.decodeFile(pathOne); ivOne.setImageBitmap(bmImg); one = bmImg; } else { pathTwo = path; Bitmap bmImg = BitmapFactory.decodeFile(pathTwo); ivTwo.setImageBitmap(bmImg); two = bmImg; } } } } if (DialogFlag == 1) { Uri uri; if (requestCode == CAMERA_RESULT) { Cursor cursor = getContentResolver().query( Media.EXTERNAL_CONTENT_URI, new String[] { Media.DATA, Media.DATE_ADDED, MediaStore.Images.ImageColumns.ORIENTATION }, Media.DATE_ADDED, null, "date_added ASC"); if (cursor != null && cursor.moveToFirst()) { do { uri = Uri.parse(cursor.getString(cursor .getColumnIndex(Media.DATA))); path = uri.toString(); } while (cursor.moveToNext()); cursor.close(); } Log.d("myLogs", path); if (btnID == 1) { pathOne = path; Bitmap bmImg = BitmapFactory.decodeFile(pathOne); ivOne.setImageBitmap(bmImg); one = bmImg; } else { pathTwo = path; Bitmap bmImg = BitmapFactory.decodeFile(pathTwo); ivTwo.setImageBitmap(bmImg); two = bmImg; } } } } @Override protected Dialog onCreateDialog(int id) { switch (id) { case IDD_TWO_BUTTONS: AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage(" ") .setCancelable(false) .setPositiveButton("ї", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { DialogFlag = 0; Intent photoPickerIntent = new Intent( Intent.ACTION_PICK); photoPickerIntent.setType("image/*"); startActivityForResult(photoPickerIntent, GALLERY_REQUEST); dialog.cancel(); } }) .setNeutralButton("", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { DialogFlag = 1; Intent cameraIntent = new Intent( MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(cameraIntent, CAMERA_RESULT); dialog.cancel(); } } ) .setNegativeButton("іі", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } } ); return builder.create(); default: return null; } }
But when I try to do this in a fragment, I get some problem.
public class MyDialogFragment extends DialogFragment { public static MyDialogFragment newInstance(int title) { MyDialogFragment frag = new MyDialogFragment(); Bundle args = new Bundle(); args.putInt("title", title); frag.setArguments(args); return frag; } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { int title = getArguments().getInt("title"); return new AlertDialog.Builder(getActivity()) .setIcon(R.drawable.plus_icon) .setTitle(title).setCancelable(false) .setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { Intent i =getActivity().getIntent(); i.putExtra("key", true); getTargetFragment().onActivityResult(getTargetRequestCode(), 101, i); } } ) .setNegativeButton("NO", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) {
onActivityResult in AddExerciseFragment
@Override public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { String path = null; switch (requestCode) { case DIALOG_FRAGMENT: boolean check = imageReturnedIntent.getBooleanExtra("key", true); if (check) { doPositiveClick(); if (resultCode == RESULT_OK && requestCode == GALLERY_REQUEST) { Uri selectedImage = imageReturnedIntent.getData(); path = getRealPathFromURI(selectedImage); Log.d("myLogs", path); if (btnID == 1) { pathOne = path; Bitmap bmImg = BitmapFactory.decodeFile(pathOne); ivOne.setImageBitmap(bmImg); one = bmImg; } else { pathTwo = path; Bitmap bmImg = BitmapFactory.decodeFile(pathTwo); ivTwo.setImageBitmap(bmImg); two = bmImg; } } } else { doNegativeClick(); Uri uri; Cursor cursor = getActivity().getContentResolver().query( Media.EXTERNAL_CONTENT_URI, new String[] { Media.DATA, Media.DATE_ADDED, MediaStore.Images.ImageColumns.ORIENTATION }, Media.DATE_ADDED, null, "date_added ASC"); if (cursor != null && cursor.moveToFirst()) { do { uri = Uri.parse(cursor.getString(cursor .getColumnIndex(Media.DATA))); path = uri.toString(); } while (cursor.moveToNext()); cursor.close(); } Log.d("myLogs", path); if (btnID == 1) { pathOne = path; Bitmap bmImg = BitmapFactory.decodeFile(pathOne); ivOne.setImageBitmap(bmImg); one = bmImg; } else { pathTwo = path; Bitmap bmImg = BitmapFactory.decodeFile(pathTwo); ivTwo.setImageBitmap(bmImg); two = bmImg; } } break; } } public void doPositiveClick() { DialogFlag = 0; Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); photoPickerIntent.setType("image/*"); startActivityForResult(photoPickerIntent, GALLERY_REQUEST);
But I have the following:
- If I want to install an image from the gallery - start the gallery, but I can not select the image there
- From the camera, I can set the image only once and for only one image.
- when I am in the gallery and press the back button, I get an error.