1

I am building a camera app that captures a photo and save's the photo to a directory with a custom name.

Problem that I am facing: Custom file is created in the directory, but photo is not captured to that file but is captured to the default /Pictures directory of the camera.

private void startCamera(int actionCode){
    //Create a camera intent
    Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    switch (actionCode) {
        case CAM_REQ_CODE:
            File photo_file = null;

            try {
                photo_file = createImageFile();
            } catch (IOException e) {
                e.printStackTrace();
                photo_file = null;
                photo_path = null;
            }

            // Continue only if the File was successfully created
            if (photo_file != null) {

                ContentValues values = new ContentValues();
                values.put(MediaStore.Images.Media.TITLE, photo_path);
                photo_uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

                camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, photo_uri);
                startActivityForResult(camera_intent, CAM_REQ_CODE);
            } break;
        default:
            break;
    }
}

A file will be created with this address : /storage/emulated/0/Pictures/IAMGROOTPics/IMG_yyyyMMdd_HHmmss_.jpg

but the file will be empty, and the actual photo is saved in /storage/emulated/0/Pictures

onActivity result codes contains

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    switch(requestCode) {
        case CAM_REQ_CODE:
            if (resultCode == RESULT_OK) {
                imageView.setVisibility(View.VISIBLE);
                if (photo_path != null) {
                    File f = new File(photo_path);
                    Uri photo_uri_2 = Uri.fromFile(f);
                    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,photo_uri_2);
                    this.sendBroadcast(mediaScanIntent);

                    photo_path = null;
                }
            }break;
        default:
            break;
    }
}

Thank you :)

Ivan
  • 311
  • 3
  • 7
  • `ContentValues values = new ContentValues(); values.put(MediaStore.Images.Media.TITLE, photo_path); photo_uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); ` Remove all that code. – greenapps Dec 13 '17 at 14:41
  • `A file will be created with this address`. You should not create a file yourself already. You only need to supply a file path/uri. – greenapps Dec 13 '17 at 14:42
  • `camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, photo_uri);`. That should be `camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo_file');` – greenapps Dec 13 '17 at 14:45

0 Answers0