将txt写入sdcard时EACCESS(权限被拒绝)

时间:2015-11-05 09:24:16

标签: android

我知道还有很多其他问题。我搜索和搜索,但没有一个答案似乎有帮助。 如果我想在我的SD卡上创建并保存.txt,我会收到以下错误:

11-05 10:13:31.244 11781-11781/com.sbeyer.daainv W/System.err: java.io.IOException: open failed: EACCES (Permission denied)
11-05 10:13:31.252 11781-11781/com.sbeyer.daainv W/System.err:     at java.io.File.createNewFile(File.java:939)
11-05 10:13:31.252 11781-11781/com.sbeyer.daainv W/System.err:     at com.sbeyer.daainv.ListViewFragment$4.onClick(ListViewFragment.java:118)
11-05 10:13:31.252 11781-11781/com.sbeyer.daainv W/System.err:     at android.view.View.performClick(View.java:5198)
11-05 10:13:31.253 11781-11781/com.sbeyer.daainv W/System.err:     at android.view.View$PerformClick.run(View.java:21147)
11-05 10:13:31.253 11781-11781/com.sbeyer.daainv W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
11-05 10:13:31.253 11781-11781/com.sbeyer.daainv W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
11-05 10:13:31.253 11781-11781/com.sbeyer.daainv W/System.err:     at android.os.Looper.loop(Looper.java:148)
11-05 10:13:31.253 11781-11781/com.sbeyer.daainv W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
11-05 10:13:31.253 11781-11781/com.sbeyer.daainv W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
11-05 10:13:31.253 11781-11781/com.sbeyer.daainv W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
11-05 10:13:31.253 11781-11781/com.sbeyer.daainv W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-05 10:13:31.253 11781-11781/com.sbeyer.daainv W/System.err: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
11-05 10:13:31.253 11781-11781/com.sbeyer.daainv W/System.err:     at libcore.io.Posix.open(Native Method)
11-05 10:13:31.253 11781-11781/com.sbeyer.daainv W/System.err:     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
11-05 10:13:31.253 11781-11781/com.sbeyer.daainv W/System.err:     at java.io.File.createNewFile(File.java:932)

我认为许可是正确的:

<manifest [...]

    <application
           [...]
        <activity
              [...]
            <intent-filter>
                  [...]
            </intent-filter>
        </activity>
    </application>

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

</manifest>

这是我的代码:

 buttonexport.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {

                    try {
                        String fileName = "DAA_Raum_Nr_" + roomnr.getText().toString() + ".txt";
                        File file = new File (Environment.getExternalStorageDirectory().getAbsoluteFile(), fileName);
                        file.createNewFile();

                        FileOutputStream fOut = new FileOutputStream(file);
                        OutputStreamWriter fos = new OutputStreamWriter(fOut);
                        fos.write(dataList);
                        fos.close();
                        fOut.close();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });

我还尝试拔掉我的设备(根据N5),但我的SD卡上也没有文件。

2 个答案:

答案 0 :(得分:1)

我将目标API更改为22以避免复杂的权限处理(Android 6)

如果没有想要名字的人,我还添加了以下代码来创建文件夹。

@Override
public void onClick(View view) {

     try {
          File myDir = new File(Environment.getExternalStorageDirectory().getAbsoluteFile()+ "/DAA_Raeume");
          if (!myDir.exists()) {
               myDir.mkdir();
               }
          String fileName = "DAA_Raum_Nr_" + roomnr.getText().toString() + ".txt";
          File file = new File (myDir, fileName);
          file.createNewFile();

          FileOutputStream fOut = new FileOutputStream(file);
          OutputStreamWriter fos = new OutputStreamWriter(fOut);
          fos.write(dataList);
          fos.close();
          fOut.close();

          } catch (Exception e) {
                e.printStackTrace();
                }
}

答案 1 :(得分:1)

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

请添加上述许可。