我正在尝试将文件从我的应用程序复制到 SD 卡,但出现错误 eacces (permission denied) 。操作系统是 Android M,我允许运行时存储权限(在应用程序信息中检查)。我还在 AndroidManifest.xml 中设置了使用权限
<application>...</application>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
如果我复制到 SD 卡,则不起作用
Source: data/user/0/com.example.myapp/cache/SomeFile.txt
Destination: /storage/1032-2568/SomeFolder/
Error: java.io.FileNotFoundException: /storage/1032-2568/SomeFolder/SomeFile.txt: open failed: EACCES (Permission denied)
如果我复制到内部存储就可以工作
Source: data/user/0/com.example.myapp/cache/SomeFile.txt
Destination: /storage/emulated/0/SomeFolder/
将文件从源复制到目标的代码
/*
* Below are the parameters I have tried
*
* inputPath - data/user/0/com.example.myapp/cache or data/user/0/com.example.myapp/cache/
* inputFile - /SomeFile.txt or SomeFile.txt
* outputPath - /storage/1032-2568/SomeFolder/ or /storage/1032-2568/SomeFolder
*/
public static void copyFile(String inputPath, String inputFile, String outputPath) {
InputStream in = null;
OutputStream out = null;
try {
//create output directory if it doesn't exist
File dir = new File (outputPath);
if (!dir.exists()) {
dir.mkdirs();
}
in = new FileInputStream(inputPath + inputFile);
out = new FileOutputStream(outputPath + inputFile);
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
// write the output file (You have now copied the file)
out.flush();
out.close();
}
catch (FileNotFoundException fnfe1) {
/* I get the error here */
Log.e("tag", fnfe1.getMessage());
}
catch (Exception e) {
Log.e("tag", e.getMessage());
}
}
ES 文件资源管理器
我看到 ES File Explorer 也无法在 Redmi 设备的 SD 卡上写入任何内容。 这是一个带有解决方案的视频。按照适用于我设备上的 ES Explorer 的步骤进行操作。这可以通过编程方式完成吗?
原文由 Sahil Khanna 发布,翻译遵循 CC BY-SA 4.0 许可协议
正如@CommonsWare 在这里所建议的,我们必须使用 android 提供的新存储访问框架,并且必须获得用户的许可才能写入 SD 卡文件,正如您所说,这已经写入文件管理器应用程序 ES 文件资源管理器中。
这是让用户选择“SD卡”的代码:
这看起来有点像这样:
并在
pickedDir
中获取文档路径,并在 copyFile 块中进一步传递并使用此路径写入文件: