How to save image to camera folder in Android Q?

VolodymyrH

I need to save an image to camera folder, but as Android Q getExternalStoragePublicDirectory is deprecated, I do it in another way. What I have (this method receive bitmap and its name):

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        ContentResolver resolver = mContext.getContentResolver();
        ContentValues contentValues = new ContentValues();
        contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, name);
        contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/png");
        contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, "DCIM/" + IMAGES_FOLDER_NAME);
        Uri imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
        OutputStream fos = resolver.openOutputStream(imageUri);
        saved = bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.flush();
        fos.close();
    } else {
        String imagesDir = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DCIM).toString() + File.separator + IMAGES_FOLDER_NAME;

        File file = new File(imagesDir);

        if (!file.exists()) {
            file.mkdir();
        }

        File image = new File(
                imagesDir,
                name + ".png"
        );

        final long fileHashCode = image.hashCode();
        Logger.d(TAG, "saveImage, saving image file, hashCode = " + fileHashCode);

        FileOutputStream fos = new FileOutputStream(image);
        saved = bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.flush();
        fos.close();
    }

That perfectly works for all needed OS versions, but it looks inaccurate and I'd like to find a more common way. I tried to play around with content values or try some similar way as for Q, but it doesn't work. I've seen many questions here, but neither of them can help me.

The question is how can I optimize saving for OS lower than Q?

VolodymyrH

The most generalized version I was able to write is:

private Uri saveImage(Context context, Bitmap bitmap, @NonNull String folderName, @NonNull String fileName) throws IOException
{
    OutputStream fos;
    File imageFile = null;
    Uri imageUri = null;


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        ContentResolver resolver = context.getContentResolver();
        ContentValues contentValues = new ContentValues();
        contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, fileName);
        contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/png");
        contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DCIM + File.separator + folderName);
        imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
        fos = resolver.openOutputStream(imageUri);
    } else {
        String imagesDir = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DCIM).toString() + File.separator + folderName;
        imageFile = new File(imagesDir);
        if (!imageFile.exists()) {
            imageFile.mkdir();
        }
        imageFile = new File(imagesDir, fileName + ".png");
        fos = new FileOutputStream(imageFile);
    }

    boolean saved = bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
    fos.flush();
    fos.close();

    if (imageFile != null)  // pre Q
    {
        MediaScannerConnection.scanFile(context, new String[]{imageFile.toString()}, null, null);
        imageUri = Uri.fromFile(imageFile);
    }

    return imageUri;
}

If you've found a better way, post here, I'll mark it as answer.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to save an image to Camera folder in android?

Can't save captured image in folder using custom camera in android

How to save an image in Android Q using MediaStore?

How to save an image in a folder?

How to save the image (taken using camera) manually in Android

Save image to a download folder on Android

How to save captured image to custom app specific folder using react-native-camera

Android camera image to a custom folder is blank

How to save image canvas to folder?

How to save create a folder and save an image in Flutter?

How to save SKSpriteNode as PNG image to Camera Roll?

How to save image to Camera Roll using Expo?

How can I save an image to the camera roll?

Save image to custom folder android on onPictureTaken

Q: On android phone, how to change folder

save camera image into directory

How to Save Image to Application Folder from UIImagePickerController?

How to auto save highcharts as image in specific folder

How to save image to user folder in django?

How to save an image to a specific folder with Cloudinary?

How to save image into resource folder java swing

how to upload image in database save in folder?

How to save image from url into assets folder

How to get and save image to folder from listview

Android : Use Camera to display image to ImageView and Save to Gallery

android not show image that I save from my custom camera app

How to save image in storage folder as well as in database when I changed profile picture in android?

How to download and save an image in Android

how to save an image file in android?