0

I am starting from my fragment the standard android camera app. Everything works fine and the image gets saved as I wish. After the user has taken a picture (and only then) I would like to launch a DialogFragment.

I tried using intents and calling onActivetyResult() from the same fragment. But onActivityResult() never gets called.

How I start the camera in my fragment:

    MyCamera camera = new MyCamera(getActivity());
    camera.start();

Im my MyCamera class I am starting the intent like so:

takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
activity.startActivityForResult(takePictureIntent, Constants.TAKE_PHOTO);

Back in the the fragment I am calling onActivityResult():

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == Constants.TAKE_PHOTO && resultCode == Activity.RESULT_OK) {
        // do something
    }
}

How can I achieve this task? Thanks in advance for the help!

Here the entire MyCamera class:

public class MyCamera {
    private Activity activity;
    private static final String TAG = "MyCamera";
    private String currentPhotoPath;
    private String currentFileName;
    private File currentFile;
    private Uri contentUri;

    public MyCamera(Activity activity) {
        this.activity = activity;
    }

    public void start() {
        dispatchPictureIntent();
        addPictureToGallery();
        currentPhotoPath = null;
    }

    private void dispatchPictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // check if device has camera
        if (takePictureIntent.resolveActivity(activity.getPackageManager()) != null) {
            File photoFile = null;
            try {
                photoFile = createImageFile();
                currentPhotoPath = photoFile.getAbsolutePath();

            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(activity, R.string.msg_picture_not_saved, Toast.LENGTH_LONG).show();
            }
            if (photoFile != null) {
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
                takePictureIntent.putExtra(Constants.IMAGE_FILE_NAME, currentFileName);
                activity.startActivityForResult(takePictureIntent, Constants.TAKE_PHOTO);
            }
        }
    }

    private File createImageFile() throws IOException {
        // set up storage dir
        File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        if (!storageDir.exists()) {
            if (!storageDir.mkdirs()) {
                Log.e(TAG, "failed to create directory");
                return null;
            }
        }
        // set up file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String fileName = "img_" + timeStamp + "_";
        String suffix = ".jpg";

        File image = File.createTempFile(fileName, suffix, storageDir);
        currentFileName = image.getName();
        currentFile = image;
        return image;
    }

    private void addPictureToGallery() {
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        contentUri = getUri(currentPhotoPath);
        mediaScanIntent.setData(contentUri);
        activity.sendBroadcast(mediaScanIntent);
    }

    private Uri getUri(String currentPhotoPath) {
        File f = new File(currentPhotoPath);
        return Uri.fromFile(f);
    }
}
  • It's because your fragment never gets the result in onActivityResult(). You have to modify your activity accordingly to this post: https://stackoverflow.com/questions/6147884/onactivityresult-is-not-being-called-in-fragment – canihazurcode Sep 15 '17 at 13:51

2 Answers2

2

Based on this post: onActivityResult is not being called in Fragment

try adding in your hosting activity

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
 }

It will allow to pass unhandled activity result to your fragment.

Also, make sure you call fragment.startActivityForResult() instead of activity.startActivityForResult().

canihazurcode
  • 252
  • 2
  • 9
1

I tried using intents and calling onActivetyResult() from the same fragment. But onActivityResult() never gets called.

That is because you are calling startActivityForResult() on the Activity, not on the fragment. Your onActivityResult() method goes on the object on which you call startActivityForResult(). So, if you want the fragment to have onActivityResult(), call startActivityForResult() on the fragment.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I am calling it on the fragment now and `onActivtityResult()` gets called. Thank you! But somehow the data intent in onActivityResult() is null... Do you know why? I am putting my data in the intent like so: `takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile)); takePictureIntent.putExtra(Constants.IMAGE_FILE_NAME, currentFileName); fragment.startActivityForResult(takePictureIntent, Constants.TAKE_PHOTO);` – Hansi Hansenbaum Sep 15 '17 at 14:28
  • @HansiHansenbaum: "But somehow the data intent in onActivityResult() is null... Do you know why?" -- it is supposed to be `null`. You know where the image is supposed to be (`photoFile`). – CommonsWare Sep 15 '17 at 14:29
  • Sorry for the stupid questions. But how would you pass a String from the intent to onActivityResult()? – Hansi Hansenbaum Sep 15 '17 at 14:41
  • @HansiHansenbaum: Hold onto it in a field of your fragment on which you have implemented `onActivityResult()`. – CommonsWare Sep 15 '17 at 14:50
  • I had to declare the attributes final because the fragment of course gets rebuilt after the picture is taken. It's not really pretty but working. Thanks for the help! – Hansi Hansenbaum Sep 16 '17 at 12:35