读取上传的文件时出现FileNotFoundException

时间:2018-12-21 17:07:19

标签: android permissions filereader

我正在尝试将IOS应用程序转换为Android。我没有Android方面的经验,所以这可能是一个愚蠢的问题。抱歉:)

我已通过设备文件浏览器将一些json文件上传到模拟器的“ files”文件夹中。 (而不是外部存储空间)

但是在阅读它们时,将引发FileNotFoundException。 (权限被拒绝)。我用于阅读的代码如下;

       try {
            File file = new File(getApplicationContext().getFilesDir() + "/Data/Users/profile.json");
            FileReader fileReader = new FileReader(file);
            String contents = "";
            int i;
            while((i = fileReader.read())!= -1) {
                char ch = (char)i;
                contents += ch;
            }
            return contents;
        } catch (IOException e) {
             e.printStackTrace();
        }

我试图以编程方式在“文件”文件夹下的同一目录中形成这些文件,如下所示。

        String string = "{}";

        File file = new File(getApplicationContext().getFilesDir() + "/Data/Users");
        file.mkdirs();

        File file2 = new File(getApplicationContext().getFilesDir() + "/Data/Users/profile.json");
        file2.createNewFile();

        FileOutputStream fos = new FileOutputStream(file2);
        fos.write(string.getBytes());
        fos.close();

这次,我设法使用上面的代码成功地阅读了它们。似乎通过设备文件资源管理器上传文件会导致一些权限问题。我找不到如何修改它们。我该如何解决?

0 个答案:

没有答案