Search code examples
javaandroidandroid-gallery

Is using getExternalStoragePublicDirectory() below android 29 safe?


So basically i'm trying to store an image to the phone storage. I have write a utility method for this purpose. (folder is the name of directory to be created)

public static void saveBitmapToGallery(Context context, Bitmap bitmap, String folder) {

        if (Build.VERSION.SDK_INT >= 29) {

            ContentValues contentValues = new ContentValues();

            contentValues.put(MediaStore.Images.Media.IS_PENDING, true);

            contentValues.put(MediaStore.Images.Media.MIME_TYPE, "image/jpg");

            contentValues.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());

            contentValues.put(MediaStore.Images.Media.DISPLAY_NAME, UUID.randomUUID() + ".jpg");

            contentValues.put(MediaStore.Images.Media.RELATIVE_PATH, Environment.DIRECTORY_PICTURES + "/" + folder);

            Uri uri = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);

            if (uri != null) {

                try {

                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, context.getContentResolver().openOutputStream(uri));

                    contentValues.put(MediaStore.Images.Media.IS_PENDING, false);

                    context.getContentResolver().update(uri, contentValues, null, null);

                } catch (FileNotFoundException e) {

                    e.printStackTrace();

                }

            }

        } else {

            // Is it safe to use deprecated method or there some alternative exist?

            File directory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/" + folder);

            boolean isDirectoryExisted = directory.exists();

            if (!isDirectoryExisted) {

                isDirectoryExisted = directory.mkdir();

            }

            if (isDirectoryExisted) {

                File imageFile = new File(directory + "/" + UUID.randomUUID().toString() + ".jpg");

                try {

                    FileOutputStream fileOutputStream = new FileOutputStream(imageFile);

                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);

                    fileOutputStream.flush();

                    fileOutputStream.close();

                } catch (Exception e) {

                    e.printStackTrace();

                }

                Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);

                mediaScanIntent.setData(Uri.fromFile(imageFile));

                context.sendBroadcast(mediaScanIntent);

            }

        }

    }

Is it safe to use this method getExternalStoragePublicDirectory() in the above scenario or not as it is deprecated. Or there is some other alternative exist?


Solution

  • This method was deprecated starting in API Level 29. It is perfectly fine to use it on older devices, subject to the standard rules for external storage (e.g., you need the WRITE_EXTERNAL_STORAGE permission, including requesting it at runtime on Android 6.0).