授予WRITE_EXTERNAL_STORAGE,尽管无法创建目录/写入文件

时间:2018-09-18 13:50:43

标签: android android-permissions android-external-storage

好,我的程序员们,

我很抱歉问这样愚蠢的事情。我敢肯定我缺少一些基本的东西。无论如何,我正在尝试将一些数据保存在外部存储中。这是我的代码:

        if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PERMISSION_GRANTED) return;
        File testDir = new File(Environment.getExternalStorageDirectory() + "/test/");
        final String externalStorageState = Environment.getExternalStorageState();
        Log.d("STATE", externalStorageState);
        if (Environment.MEDIA_MOUNTED.equals(externalStorageState)) {
            Log.d("PATH", testDir.getAbsolutePath());
            if (!testDir.exists()) {
                Log.d("MAKE DIRS", testDir.mkdirs() + "");
                Log.d("MAKE DIR", testDir.mkdir() + "");
            }
            File aFile = new File(testDir, "somefile");
            FileOutputStream fos = new FileOutputStream(aFile);
            fos.write("data".getBytes());
            fos.close();
        }

日志在这里:

D/STATE: mounted
D/PATH: /storage/emulated/0/test
D/MAKE DIRS: false
D/MAKE DIR: false

问题是没有创建目录,因此FileOutputStream引发IOException。

0 个答案:

没有答案