API级别29中不推荐使用Environment.getExternalStorageDirectory() [英] Environment.getExternalStorageDirectory() deprecated in API level 29 java
问题描述
使用android Java进行工作,最近将SDK更新到API级别29,现在显示警告,指出
Working on android Java, recently updated SDK to API level 29 now there is a warning shown which states that
Environment.getExternalStorageDirectory()
在API级别29中已弃用
Environment.getExternalStorageDirectory()
is deprecated in API level 29
我的代码是
private void saveImage() {
if (requestPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
final String folderPath = Environment.getExternalStorageDirectory() + "/PhotoEditors";
File folder = new File(folderPath);
if (!folder.exists()) {
File wallpaperDirectory = new File(folderPath);
wallpaperDirectory.mkdirs();
}
showLoading("Saving...");
final String filepath=folderPath
+ File.separator + ""
+ System.currentTimeMillis() + ".png";
File file = new File(filepath);
try {
file.createNewFile();
SaveSettings saveSettings = new SaveSettings.Builder()
.setClearViewsEnabled(true)
.setTransparencyEnabled(true)
.build();
if(isStoragePermissionGranted() ) {
mPhotoEditor.saveAsFile(file.getAbsolutePath(), saveSettings, new PhotoEditor.OnSaveListener() {
@Override
public void onSuccess(@NonNull String imagePath) {
hideLoading();
showSnackbar("Image Saved Successfully");
mPhotoEditorView.getSource().setImageURI(Uri.fromFile(new File(imagePath)));
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,Uri.fromFile(new File(filepath))));
Intent intent = new Intent(EditImageActivity.this, StartActivity.class);
startActivity(intent);
finish();
}
@Override
public void onFailure(@NonNull Exception exception) {
hideLoading();
showSnackbar("Failed to save Image");
}
});
}
这有什么选择?
推荐答案
使用 getExternalFilesDir()
, getExternalCacheDir()
或 getExternalMediaDir()
( Context
上的方法),而不是 Environment.getExternalStorageDirectory()
。
Use getExternalFilesDir()
, getExternalCacheDir()
, or getExternalMediaDir()
(methods on Context
) instead of Environment.getExternalStorageDirectory()
.
或者,修改 mPhotoEditor
使其能够与 Uri
,然后:
Or, modify mPhotoEditor
to be able to work with a Uri
, then:
-
使用
ACTION_CREATE_DOCUMENT
获取Uri
到用户选择的位置,或
Use
ACTION_CREATE_DOCUMENT
to get aUri
to a location of the user's choosing, or
使用 MediaStore
, ContentResolver
和 insert()
以获得 Uri
用于特定类型的媒体(例如图像)—请参见此示例应用,该示例演示了如何下载MP4来自网站的视频
Use MediaStore
, ContentResolver
, and insert()
to get a Uri
for a particular type of media (e.g., an image) — see this sample app that demonstrates doing this for downloading MP4 videos from a Web site
此外,请注意,您的 Uri.fromFile $ c具有
ACTION_MEDIA_SCANNER_SCAN_FILE
的$ c>在Android 7.0及更高版本上应具有 FileUriExposedException
崩溃。在Android Q上,只有 MediaStore
/ insert()
选项将使您的内容由 MediaStore
很快。
Also, note that your Uri.fromFile
with ACTION_MEDIA_SCANNER_SCAN_FILE
should be crashing on Android 7.0+ with a FileUriExposedException
. On Android Q, only the MediaStore
/insert()
option will get your content indexed by the MediaStore
quickly.
请参见此博客文章,详细了解Android Q如何影响外部存储。
See this blog post for more on how Android Q has affected external storage.
这篇关于API级别29中不推荐使用Environment.getExternalStorageDirectory()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!