5

I'm using a simple camera intent following the basic tutorial from Android. In this section it talks about saving the image file to disk. However, I haven't yet configured any of these steps, but after capturing the image and returning to my activity, it's still automatically saving my image to disk in /storage/emaulated/0/DCIM/Camera. This doesn't seem to be what's implied by the tutorial - I don't even have the WRITE_EXTERNAL_STORAGE permission in my manifest so I'm not sure why it's even allowed to write to disk. I don't want the image to automatically save to this directory, but rather to a directory of my choosing. I know how to save the image to a custom directory, but how can I prevent the default behavior of saving the image to the directory above?

ethan123
  • 1,084
  • 2
  • 14
  • 26
  • Facing same problem. Did you get the solution? – Ankur Raiyani Dec 04 '15 at 14:15
  • 1
    so basically there is no "solution" because actually what is happening is intended by Android it seems. when we use the code from this tutorial, we are simply utilizing the stock Camera app on your device. since we don't control this app, we have no way of preventing its built-in behavior, which is to save photos to this directory. you can either ALSO save to a custom directory as is described in other answers or try your hand implementing a custom camera, which is, to say the least, complicated – ethan123 Dec 05 '15 at 10:29
  • Check this reply, that I got for the same problem: https://stackoverflow.com/a/57660942/9242141 – Ziad H. Dec 25 '19 at 16:52

4 Answers4

1

When you try to take a phto, actually you are start calling Camera App installed in your device. You didn't set WRITE_EXTERNAL_STORAGE, Oh yes, but the App of Camera is set. And when you try to take a picture but want to store the photo into your file, you could try this:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra("return-data", false);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
                Uri.fromFile(AVATAR_FILE_TMP));
intent.putExtra("outputFormat",
                Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true);
startActivityForResult(intent, TAKING_PICTURE_INDEX);

And filePath is the path of the image file you take by Camera. Taking a photo uses Camera App.

SilentKnight
  • 13,761
  • 19
  • 49
  • 78
  • so if i'm following this correctly, what's happening for me is that the new directory is being created, but the image file is not being written properly Dinesh's tip is working for writing the image file to the directory but not preventing the file from also being written to the original directory – ethan123 Apr 20 '15 at 11:01
  • cool, thanks still not quite getting the desired effect. which part of this is actually preventing the Camera app from writing to its own folder. also, what's meant by the noFaceDetection part? my desired behavior is to only have the file written to the location i specified and NOT to the default storage/emulated/0/DCIM/Camera folder. is this possible? – ethan123 Apr 20 '15 at 11:58
  • then you don't set `outputFormat` and `noFaceDetection` – SilentKnight Apr 21 '15 at 01:37
0

If you use the following code, It will not save the default camera roll.

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, receiptUri);
startActivityForResult(takePictureIntent, 1);

But you need WRITE permission in your manifest.

Dinesh T A
  • 2,087
  • 4
  • 26
  • 34
  • 3
    hey dinesh, with this code it's still saving to the default Camera folder, but is also being written to my custom folder as well. anyway to prevent it from writing to the default folder and JUST to my custom folder? – ethan123 Apr 20 '15 at 11:59
0

Here is a method that creates a folder with the name for your app in the "pictures" folder on your SD card. You can change it so that it reflects your needs.

    // Create a File for saving an image or video
    private static File getOutputMediaFile(int type){
        // To be safe, you should check that the SDCard is mounted
        // using Environment.getExternalStorageState() before doing this.

        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                  Environment.DIRECTORY_PICTURES), "MyAppDirectory");
        // This location works best if you want the created images to be shared
        // between applications and persist after your app has been uninstalled.

        // Create the storage directory if it does not exist
        if (! mediaStorageDir.exists()){
            if (! mediaStorageDir.mkdirs()){
                Log.d("MyAppDirectory", "failed to create directory");
                return null;
            }
        }
            // Create a media file name
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            File mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                "IMG_"+ timeStamp + ".jpg");

            return mediaFile;
}

You can call this method when you need it:

        File file = getOutputMediaFile(MEDIA_TYPE_IMAGE);
panonski
  • 555
  • 5
  • 10
0

comment this line if have

MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
swati kapoor
  • 131
  • 13