Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save an image to Camera folder in android?

Tags:

java

android

I am trying to save a photo to Camera folder which my app takes. This is what I am trying

private File createImageFile() throws IOException {

    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "PlumFastJob" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory("Camera");

    if(!storageDir.exists()){

        boolean s = new File(storageDir.getPath()).mkdirs();

        if(!s){
            Log.v("not", "not created");
        }
        else{
            Log.v("cr","directory created");
        }
    }
    else{
        Log.v("directory", "directory exists");
    }

    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".png",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    currentPhotoPath = image.getAbsolutePath();
    uriOfImage = Uri.parse(image.getPath());
    return image;
}

I have also tried Enviornment.Directory_DCIM and Environment.Directory_Pictures. But both of them creates a folder called DCIM and Pictures.

This just creates another folder called Camera and saves it.

Here is my file provider.

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="my_images" path="." />
</PreferenceScreen>
like image 345
alan samuel Avatar asked Oct 15 '25 16:10

alan samuel


2 Answers

All camera apps nowadays save to a Camera directory inside the DCIM directory.

So you have to use

File dir = new File( Environment.getExternalStoragePublicDirectory(
      Environment.DIRECTORY_DCIM), "Camera");
like image 109
greenapps Avatar answered Oct 18 '25 07:10

greenapps


You are getting path File storageDir Environment.getExternalStoragePublicDirectory("Camera"); this will create the folder with name of "Camera"

DIRECTORY_DCIM The traditional location for pictures and videos when mounting the device as a camera.

File musicDirectory = new File( getExternalFilesDir(Environment.DIRECTORY_DCIM));

DIRECTORY_PICTURES Standard directory in which to place pictures that are available to the user.

File musicDirectory = new File( getExternalFilesDir(Environment.DIRECTORY_PICTURES));

Hope it will help you!!

like image 36
Hemant Parmar Avatar answered Oct 18 '25 06:10

Hemant Parmar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!