我想从外部存储中读取文件。我将Eclipse DDMS中的文件上传到storage / sdcard(下图)。但每当我试图阅读时,我都会收到拒绝许可的错误。我的文件权限有问题吗?或者我需要在清单文件中添加任何内容(我现在不写任何内容)?
任何帮助将不胜感激。
代码:
public void extimport(View v){
EditText xedittxt = (EditText) findViewById(R.id.frmexttxt);
String xedit = xedittxt.getText().toString();
xedit = xedit.trim();
File file;
file = new File(xedit);
StringBuilder text = new StringBuilder();
Log.d("fcheck",""+xedit);
try {
BufferedReader br = new BufferedReader(new FileReader(file)); //THIS LINE THROWS ERROR
Log.d("fcheck","f3"); //This line never got printed
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
br.close();
resultView = (TextView) findViewById(R.id.header);
resultView.setText(text);
}
catch (IOException e) {
Log.d("File open error",""+e);
Toast.makeText(getApplicationContext(), "Error opening the file.", Toast.LENGTH_SHORT).show();
}
}
logcat的:
11-19 00:21:54.252: D/fcheck(5885): /storage/sdcard/mylibman.csv
11-19 00:21:54.272: D/File open error(5885): java.io.FileNotFoundException: /storage/sdcard/mylibman.csv: open failed: EACCES (Permission denied)
答案 0 :(得分:45)
确保您已在清单文件中添加了读取外部存储的权限。
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
答案 1 :(得分:2)
我遇到了同样的问题,可以通过以下任一步骤轻松删除它: 1.如果在Android N和O版本上安装,请使用-g安装您的应用程序。 2.手动授予权限 设置 - &gt; apps-&gt;“app_name” - &gt;权限 - &gt;启用开关
对于步骤1和步骤2,在AndroidManifest.xml中定义uses-permission android:name =“android.permission.WRITE_EXTERNAL_STORAGE”并使用权限android:name =“android.permission.READ_EXTERNAL_STORAGE”
答案 2 :(得分:0)
在访问功能之前,需要由用户授予存储权限
添加依赖性:
dependencies:
permission_handler: ^5.0.1
代码段:
var status = await Permission.storage.status;
if (status.isUndetermined) {
// You can request multiple permissions at once.
Map<Permission, PermissionStatus> statuses = await [
Permission.storage,
].request();
print(statuses[Permission.storage]); // it should print PermissionStatus.granted
}