使用getExternalStoragePublicDirectory()在Android 29以下安全吗?

huangapple go评论71阅读模式
英文:

Is using getExternalStoragePublicDirectory() below android 29 safe?

问题

基本上,我正在尝试将图像存储到手机存储中。我已经编写了一个用于此目的的实用方法。
(“folder”是要创建的目录的名称)

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 {

            // 在上述情况下使用此方法getExternalStoragePublicDirectory()是否安全,因为它已被弃用?还是存在其他替代方法?

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

            }

        }

    }

在上述情况下,是否安全使用此方法getExternalStoragePublicDirectory(),因为它已被弃用?还是存在其他替代方法?

英文:

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?

答案1

得分: 2

这个方法从API级别29开始已被弃用。在较旧的设备上使用它是完全可以的,但需要遵守外部存储的标准规则(例如,您需要WRITE_EXTERNAL_STORAGE权限,包括在Android 6.0上运行时请求它)。

英文:

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).

huangapple
  • 本文由 发表于 2020年8月10日 06:09:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63331802.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定