将数据保存在外部存储中。 FileNotFoundException异常

时间:2017-11-30 08:04:18

标签: android caching filenotfoundexception android-external-storage

我正在尝试将数据保存在外部存储中。但我得到一个FileNotFoundException。

我在线查找并在Manifest中包含了所需的权限,并通过' mkdirs()'创建了该文件夹。我正在模拟器上测试它 - Nexus 6 API 23。

你能否建议我在哪里做错了。

 @Override
    public void saveExternalPublic(String data) {
        File folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        File myFile = new File(folder, "myData4.txt");
        folder.mkdirs();
        writeData(myFile, data);
    }

 @Override
    public void writeData(File myFile, String data) {
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(myFile);
            fileOutputStream.write(data.getBytes());
            cacheSaveView.showMessage(data + " was written successfully "+ myFile.getAbsolutePath());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            if(null != fileOutputStream) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

权限

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

错误日志

11-30 00:51:43.178 23490-23490/com.example.rao.cachingsample W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Download/myData4.txt: open failed: EACCES (Permission denied)
11-30 00:51:43.180 23490-23490/com.example.rao.cachingsample W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:452)
11-30 00:51:43.180 23490-23490/com.example.rao.cachingsample W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
11-30 00:51:43.180 23490-23490/com.example.rao.cachingsample W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
11-30 00:51:43.181 23490-23490/com.example.rao.cachingsample W/System.err:     at com.example.rao.cachingsample.Caching.Presenter.CacheSavePresenterImpl.writeData(CacheSavePresenterImpl.java:60)
11-30 00:51:43.181 23490-23490/com.example.rao.cachingsample W/System.err:     at com.example.rao.cachingsample.Caching.Presenter.CacheSavePresenterImpl.saveExternalPublic(CacheSavePresenterImpl.java:53)
11-30 00:51:43.181 23490-23490/com.example.rao.cachingsample W/System.err:     at com.example.rao.cachingsample.Caching.View.FCacheSave.saveExternalPublic(FCacheSave.java:82)
11-30 00:51:43.181 23490-23490/com.example.rao.cachingsample W/System.err:     at com.example.rao.cachingsample.Caching.View.FCacheSave_ViewBinding$4.doClick(FCacheSave_ViewBinding.java:63)
11-30 00:51:43.182 23490-23490/com.example.rao.cachingsample W/System.err:     at butterknife.internal.DebouncingOnClickListener.onClick(DebouncingOnClickListener.java:22)
11-30 00:51:43.182 23490-23490/com.example.rao.cachingsample W/System.err:     at android.view.View.performClick(View.java:5198)
11-30 00:51:43.183 23490-23490/com.example.rao.cachingsample W/System.err:     at android.view.View$PerformClick.run(View.java:21147)
11-30 00:51:43.184 23490-23490/com.example.rao.cachingsample W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
11-30 00:51:43.185 23490-23490/com.example.rao.cachingsample W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
11-30 00:51:43.185 23490-23490/com.example.rao.cachingsample W/System.err:     at android.os.Looper.loop(Looper.java:148)
11-30 00:51:43.185 23490-23490/com.example.rao.cachingsample W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
11-30 00:51:43.192 23490-23490/com.example.rao.cachingsample W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
11-30 00:51:43.193 23490-23490/com.example.rao.cachingsample W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
11-30 00:51:43.198 23490-23490/com.example.rao.cachingsample W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-30 00:51:43.199 23490-23490/com.example.rao.cachingsample W/System.err: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
11-30 00:51:43.211 23490-23490/com.example.rao.cachingsample W/System.err:     at libcore.io.Posix.open(Native Method)
11-30 00:51:43.215 23490-23490/com.example.rao.cachingsample W/System.err:     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
11-30 00:51:43.216 23490-23490/com.example.rao.cachingsample W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:438)

4 个答案:

答案 0 :(得分:0)

您应首先创建目录,然后创建文件。

    File folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
    folder.mkdirs();
    File myFile = new File(folder, "myData4.txt");
    writeData(myFile, data);

答案 1 :(得分:0)

转到此链接获取运行时权限。 https://www.androidhive.info/2016/11/android-working-marshmallow-m-runtime-permissions/

从API 23起意味着Android 6.0(棒棒糖)只有Manifest Permission不足以访问任何权限。您必须提供运行时权限对话框,以便在相关屏幕上询问用户是否允许。

答案 2 :(得分:0)

从Android 6开始,您必须在运行时请求权限。查看更多here

进行测试,您可以自己启用它们。您选择设置 - &gt;应用 - >选择您的应用 - >选择权限部分 - &gt;启用所有权限

了解在运行时需要请求的权限。你应该看看my answer

答案 3 :(得分:0)

你需要在运行时在 android API 23 + 中询问权限,即使你已经在清单中声明了它们

public static void StoragePermissions(Activity activity) {
// Check if we have write permission
int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

if (permission != PackageManager.PERMISSION_GRANTED) {
    // We don't have permission so prompt the user
    ActivityCompat.requestPermissions(activity,PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE
    );
   }
 }

还要检查您是否已在应用程序标记中声明了清单权限。通过以下链接查看更多信息: Exception 'open failed: EACCES (Permission denied)' on Android 还有更多Android Docs