在 android 29 以下使用 getExternalStoragePublicDirectory() 安全吗?
Is using getExternalStoragePublicDirectory() below android 29 safe?
所以基本上我正在尝试将图像存储到 phone 存储。为此,我编写了一个实用方法。
(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 {
// 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);
}
}
}
在上述情况下使用此方法 getExternalStoragePublicDirectory() 是否安全,因为它已被弃用。或者还有其他选择吗?
此方法从 API 级别 29 开始弃用。在旧设备上使用它完全没问题,但要遵守外部存储的标准规则(例如,您需要 WRITE_EXTERNAL_STORAGE
权限,包括在 Android 6.0).
运行时请求它
所以基本上我正在尝试将图像存储到 phone 存储。为此,我编写了一个实用方法。
(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 {
// 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);
}
}
}
在上述情况下使用此方法 getExternalStoragePublicDirectory() 是否安全,因为它已被弃用。或者还有其他选择吗?
此方法从 API 级别 29 开始弃用。在旧设备上使用它完全没问题,但要遵守外部存储的标准规则(例如,您需要 WRITE_EXTERNAL_STORAGE
权限,包括在 Android 6.0).