我有一个简单的应用程序正在尝试完成:
//列出txt文件中的所有SD卡文件名。

而且,我尝试了很多方法,至少在根存储目录中创建文件,但是没有运气(不是在使用“ getFilesDir()”的应用程序私有文件中),这些示例之一已在代码示例中显示。
是的,我使用了适当的权限。

任何帮助将不胜感激。

String path = Environment.getExternalStorageDirectory().getAbsolutePath();
    File file = new File(path, "Something.txt");
    file.mkdirs();

    String[] array = file.list();

    try {

        FileOutputStream fos = new FileOutputStream(file, true);
        OutputStreamWriter osw = new OutputStreamWriter(fos);
        osw.write(String.valueOf(array));
        osw.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

最佳答案

如果要写入外部存储,请将此权限添加到清单文件中:

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

根据此[条款] [1]

关于java - 无法在存储根目录中创建文件?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35116654/

10-12 00:23
查看更多