android - 这段代码过时了吗?获取外部存储公共目录()
问题描述
为什么这段代码是我画白线的问题 是什么问题 有没有办法替换呢?
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
正是这行代码是用白线法画出来的
getExternalStoragePublicDirectory
解决方案
getExternalStoragePublicDirectory()
-->在 API 级别 29 中已弃用
为了提高用户隐私,不推荐直接访问共享/外部存储设备。当应用程序以 Build.VERSION_CODES.Q 为目标时,从此方法返回的路径不再可供应用程序直接访问。
Context#getExternalFilesDir(String)
通过迁移到MediaStore 或 Intent#ACTION_OPEN_DOCUMENT等替代方案,应用程序可以继续访问存储在共享/外部存储上的内容 。
void createExternalStoragePrivateFile() {
// Create a path where we will place our private file on external
// storage.
File file = new File(getExternalFilesDir(null), "DemoFile.jpg");
try {
// Very simple code to copy a picture from the application's
// resource into the external file. Note that this code does
// no error checking, and assumes the picture is small (does not
// try to copy it in chunks). Note that if external storage is
// not currently mounted this will silently fail.
InputStream is = getResources().openRawResource(R.drawable.balloons);
OutputStream os = new FileOutputStream(file);
byte[] data = new byte[is.available()];
is.read(data);
os.write(data);
is.close();
os.close();
} catch (IOException e) {
// Unable to create file, likely because external storage is
// not currently mounted.
Log.w("ExternalStorage", "Error writing " + file, e);
}
}
推荐阅读
- excel - 在 Excel 中使用高级查找函数
- google-apps-script - Google Apps 脚本:未定义参考错误 getValue
- pdf - Ghostscript 不会压缩所有 PDF
- c# - 如何将自 1970 年 1 月 1 日以来的秒数转换为日期时间值?
- angular - 使用 Azure AD B2C 时向 Angular SPA 和 MSAL 添加角色
- asp.net-core - 如何在 asp.net core 2.2 中更改 api 返回结果?
- android - 无法使用 Android Studio 运行项目
- java - 当仅使用一个单元格时,如何在 Miglayout 中强制每行 4 个单元格等宽?
- python - 如何识别列表中的重复字符串?
- vsto - VSTO Msproject:锁定任务