在外部SD卡中写入文件时,我收到错误EACCESS权限被拒绝.我已经设置了权限
< uses-permission android:name =“android.permission.WRITE_EXTERNAL_STORAGE”/>
但是当我读取文件时,我成功地能够读取它但无法写入文件.我用于在SD卡中写入文件的代码是:
< 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中的讨论.
如果是这种情况,那么普通的应用程序似乎不可能在没有平台签名的情况下向真实的SD卡写入任何东西……