简体   繁体   中英

How can i save captured image to gallery?

I have some codes like this for saving image to gallery,

But it says deprecated for "getExternalStoragePublicDirectory"

I know i need to use MediaStore or getExternalFilesDir or something

But don't know how to

Thanks

private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

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

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

try this for new Function getExternalFilesDir

File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);

Just change this line of code:

File storageDir = getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

To:

File storageDir = applicationContext().getFilesDir()

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