'Storing images in Android 11 and above usign android java

I am developing an android app using java and I want to save captured image from my CameraActivity into phone storage. Everything is working fine until Android 10 but when I run the app on Android 11 and above, I can not save the image in the storage. I have read about it and android docs said that from android 11 and onwards, you have to use Scoped Storage and for that I am using MediaStore for storing the images but it gives me the following error:

/external/file/DemoPicture.jpeg: open failed: ENOENT (No such file or directory)

Here is my code to save the image in Android 11 and above:

File picture;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    ContentResolver resolver = activity.getContentResolver();
                    ContentValues contentValues = new ContentValues();
                    contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, "DemoPicture.jpeg");
                    contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg");
                    contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH,
                            Environment.DIRECTORY_PICTURES
                                    + File.separator
                                    + getResources().getString(R.string.app_name)
                                    + File.separator + "imgfolder");
                    contentValues.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis() / 1000);
                    contentValues.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
                    contentValues.put(MediaStore.MediaColumns.IS_PENDING, 1);
                    Uri imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
                    OutputStream outputStream = resolver.openOutputStream(Objects.requireNonNull(imageUri));

                    try {
                        outputStream.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        contentValues.clear();
                        contentValues.put(MediaStore.MediaColumns.IS_PENDING, 0);
                        resolver.update(imageUri, contentValues, null, null);
                    }

                    Uri external = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                    picture = new File(external.getPath(), "DemoPicture.jpg");


                } else {
                    File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
                    picture = new File(path, "DemoPicture.jpg");

                    path.mkdirs();

                    output = new FileOutputStream(picture);
                    output.write(bytes);

                }


                File newFile = new File(picture.getAbsolutePath()); // this line gives me the error


Solution 1:[1]

After spending hours, I finally got it working. No need to use MediaStore Api. Just use getExternalFilesDir(null) which returns the complete path of your app and save your image in that path.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sallu Fiverr