1

I'm launching the camera from my app to take a photo. It also becomes available in the gallery.

The common issue with this, is how to know the path of the photo. The proposed solutions are:

  1. save the path yourself, and send it over using EXTRA_OUTPUT
  2. take the path from the last taken photo in the gallery.

Solution 1 isn't reliable.

I'm trying to make solution 2 work, with this code:

public static String getTakenPhotoPath(Context context) {
    try {
        String[] projection = {MediaStore.Images.Media.DATA};
        Cursor cursor = context.getContentResolver().query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                projection, null, null, null);

        int column_index_data = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToLast();

        return cursor.getString(column_index_data);
    } catch (Exception ex) {
        ex.printStackTrace();

        return null;
    }
}

This doesn't return the path of the latest photo; it returns the previous one. Before asking for the path of the latest photo, I'm doing this:

private void addToGallery(Uri urlType, String path) {
    if (!Strings.isNullOrEmpty(path)) {
        ContentValues values = new ContentValues();
        // Add the date meta data to ensure the image is added at the front of the gallery
        values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis());
        values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
        values.put(MediaStore.MediaColumns.DATA, path);

        getContentResolver().insert(urlType, values);

        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        Uri contentUri = Uri.fromFile(new File(path));
        mediaScanIntent.setData(contentUri);
        sendBroadcast(mediaScanIntent);
    }
}
Community
  • 1
  • 1
RominaV
  • 3,335
  • 1
  • 29
  • 59
  • Solution 2 is less reliable than Solution 1, by a wide margin. There is no requirement for a camera app to have the `MediaStore` index the image, let alone do so in a timely fashion. See [this answer](https://stackoverflow.com/a/41945392/115145) for my recommended `ACTION_IMAGE_CAPTURE` triage. – CommonsWare Feb 03 '17 at 17:43
  • Solution 1 is reliable. I've never seen it fail. – greenapps Feb 03 '17 at 17:44
  • @greenapps: You haven't seen all the people here on Stack Overflow reporting problems from various camera apps. `ACTION_IMAGE_CAPTURE` simply isn't tested much, and some developers don't read the documentation for how `ACTION_IMAGE_CAPTURE` is supposed to work. See [this rant of mine from 2015](https://commonsware.com/blog/2015/06/08/action-image-capture-fallacy.html) for more. – CommonsWare Feb 03 '17 at 17:45
  • Why don't you just call your `Uri.getPath()` in `onActivityResult` – Pztar Feb 03 '17 at 17:48
  • @CommonsWare isn't `CWAC cam -2 ` a better choice? – OBX Feb 03 '17 at 17:54
  • @OBX: It is *a* choice. "Better" requires criteria, and there are plenty of criteria in which CWAC-Cam2 is not a better choice. For example, if a criterion is "must have been developed by people with full heads of hair", CWAC-Cam2 sucks. :-) – CommonsWare Feb 03 '17 at 18:30

1 Answers1

0

You can it do like this: When you are opening the camera, you can pass your own preferred location for future captured image like this in given code. And in onActivityResult you can directly use that image path for your use.

 File image ;
    Intent photoCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            File imagesFolder = new    File(Environment.getExternalStorageDirectory(), "MyImages");
            imagesFolder.mkdirs(); // <----
            image = new File(imagesFolder, getFileName());
            Uri uriSavedImage = Uri.fromFile(image);
            photoCaptureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
            startActivityForResult(photoCaptureIntent, PICK_FROM_CAMERA);


    private String getFileName() {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
            Date now = new Date();
            String fileName = formatter.format(now) + ".jpg";
            return fileName;
        }


@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        try {
            setResult(resultCode, data);


            switch (requestCode) {
                case PICK_FROM_CAMERA:
                    try {
                        if (resultCode != RESULT_CANCELED) {
                            Intent upload = new Intent(EditProfile.this, ImageCropingFucAtivity.class);
                            prefs.edit().putBoolean("FromCamera", true).commit();
                            upload.putExtra(CropImage.IMAGE_PATH, image.toString());
                            upload.putExtra(CropImage.SCALE, true);
                            upload.putExtra(CropImage.ASPECT_X, 2);
                            upload.putExtra(CropImage.ASPECT_Y, 2);
                            startActivity(upload);
                            imageSelected = true;
                            finish();
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    break;

                  }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
marienke
  • 2,465
  • 4
  • 34
  • 66
Lovekush Vishwakarma
  • 3,035
  • 23
  • 25