StartActivityForResult of Fragmnet

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) { //AddExerciseFragment.doNegativeClick(); Intent i =getActivity().getIntent(); i.putExtra("key", false); getTargetFragment().onActivityResult(getTargetRequestCode(), 101, i); } } ) .create(); } } 

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); // dialog.cancel(); } public void doNegativeClick() { DialogFlag = 1; Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(cameraIntent, CAMERA_RESULT); } void showDialog() { DialogFragment newFragment = MyDialogFragment .newInstance(R.string.name); newFragment.show(getFragmentManager(), "dialog"); } 

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.
0
android android-activity android-fragments dialog
Jul 31 '13 at
source share

No one has answered this question yet.

See similar questions:

2
Fragments, how to start a dialogue from a fragment?

or similar:

2735
Stop EditText from getting focus when starting Activity
831
How to control startActivityForResult on Android?
3
How to send an image from one activity to another in Android?
3
State of calling activity after pressing StartActivityForResult and Home
2
Android cannot shut down
2
Problem with intent, return value (Android)
one
How to delete an item in ListView and database - Android Studio
one
Intent call using startActivityForResult in the onActivityResult method and how to process the result
0
In TabHost onActivityResult not working
-one
Android: view multiple images



All Articles