在外部 SD 卡中写入文件时出现 EACCESS 权限被拒绝的错误。我设置了权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
但是当我读取文件时,我可以成功读取它但无法写入文件。我用于将文件写入 SD 卡的代码是:
String path="mnt/extsd/Test";
try{
File myFile = new File(path, "Hello.txt"); //device.txt
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
myOutWriter.append(txtData.getText());
myOutWriter.close();
fOut.close();
Toast.makeText(getBaseContext(),"Done writing SD "+myFile.getPath(),Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),Toast.LENGTH_SHORT).show();
System.out.println("Hello"+e.getMessage());
}
}
外部存储卡的路径是mnt/extsd/
.这就是为什么我无法使用 Environment.getExternalStorageDirectory().getAbsolutePath()
这给了我一条路 mnt/sdcard
此路径用于我的平板电脑中的内部存储路径。请提出为什么会这样,我该如何解决
最佳答案
我记得自 Honeycomb 以来,Android 获得了部分多存储支持,并且主要存储(您从 Environment.getExternalStorageDirectory 获得的那个,通常是内部 eMMC 卡的一部分)仍然受到 WRITE_EXTERNAL_STORAGE< 权限的保护
,但辅助存储(如真正的可移动 SD 卡)受新权限 android.permission.WRITE_MEDIA_STORAGE
保护,保护级别为 signatureOrSystem,另请参阅 this article 中的讨论。 .
如果是这种情况,那么普通应用程序似乎不可能在没有平台签名的情况下将任何内容写入真正的 sdcard...
关于android - Android 中的 EACCESS 权限被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12853752/