我正在开发我的第一个应用程序,我必须从存储卡中读取一个txt文件。我似乎无法获得/ storage中任何目录或文件的读取权限。 我正在使用https://github.com/vaal12/AndroidFileBrowser中的FileBrowserActivity来获取文件选择的GUI。我已经尝试从GUI,只是将路径传递给文件,但没有一个工作。这就是我所拥有的:
InputStream is = null;
File dir = Environment.getExternalStorageDirectory();
File f_path = new File(filepath);//dir,
Log.i(TAGraph, "file exists: " + f_path.exists() + " isDirectory: " + f_path.isDirectory() + " read: " + f_path.canRead());
if(f_path.exists() && !f_path.isDirectory() && f_path.canRead()) {
Log.i(TAGraph, "file from user1");
is = new BufferedInputStream(new FileInputStream(f_path));
Log.i(TAGraph, "file from user2");
}
else{
is = getAssets().open("DATA.txt");
Log.i(TAGraph, "file from Assets 3");
}
来自Logcat:
11-24 16:15:29.636 31337-31337/com.elcheapo.ashplot I/tagraph: on FILE try1. filepath: /storage/DATA.txt
11-24 16:15:29.643 31337-31337/com.elcheapo.ashplot I/tagraph: file exists: true isDirectory: false read: false
11-24 16:15:29.643 31337-31337/com.elcheapo.ashplot I/tagraph: file from Assets 3
的AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18" />
我尝试过只读,读和写以及有和没有SdkVersion。
我总是可以从资产中获取文件,而不是从存储中获取。文件在那里。我已尝试在模拟器和LG G5上进行测试。 我从mainActivity获取文件路径。我已经检查过,路径通过正常。(不知道Activity to Activity权限是否发生变化)