简体   繁体   中英

How to save an image to Camera folder in 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>

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");

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!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM