我正在尝试将公共文件保存在外部存储上。我正在关注 Android 开发者页面中的示例: https ://developer.android.com/training/data-storage/files#PublicFiles
首先,我尝试在公共目录 DIRECTORY_DOCUMENTS 中创建一个目录“mydocuments”。代码很简单
TextView tv= findViewById(R.id.myTextview);
// Get the directory for the user's public pictures directory.
File documents= Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOCUMENTS);
tv.append("\n documents directory=" + documents);
File file = new File(documents, "mydocuments");
tv.append("\n\nDirectory to be created=" + file);
if (file.exists())
tv.append("\n\nFile already exists:" + file.getAbsolutePath());
else{
if (!file.mkdirs())
tv.append("\n\nDirectory not created:" + file.getAbsolutePath());
我在清单文件中添加了权限
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE">
</uses-permission>
但是,当我在设备(虚拟或物理)中运行它时,结果总是相同的:
documents directory=/storage/emulated/0/Documents
Directory to be created=/storage/emulated/0/Documents/mydocuments
Directory not created:/storage/emulated/0/Download/mydocuments
如何使 Documents 目录可写?
先感谢您。