我试图从第三方应用创建的SD卡中读取文件,但我收到错误消息:
I/System.out: Exception java.io.FileNotFoundException: /storage/emulated/0/export.csv: open failed: EACCES (Permission denied)
我在运行Marshmallow的华硕Nexus 7平板电脑上安装了Android Studio。
我的清单文件包括:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test.imagechooser">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application....
我加了
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
到我的MainActivity.java文件。 获取文件的代码是:
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard,"export.csv");
System.out.println(file.toString());
System.out.println(file.exists());
if (file.exists()) System.out.println(file.canRead());
checkExternalMedia();
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
...
br.close();
}
catch (IOException e) {
System.out.println("Exception " + e);
}
从Write a file in external storage in Android复制checkExternalMedia
方法,并声称读取和写入权限设置为true。但是,当我的file.exists()
返回true时,file.canRead()
将返回false和BufferedReader
语句
导致FileNotFoundException
。有人建议断开与PC的连接或向/
添加export.csv
,但这些都没有任何效果。还有其他想法吗?