简体   繁体   English

Android FilesDir() 上的文件权限问题

[英]Android File Permissions issues on FilesDir()

I need to use a file in my application.我需要在我的应用程序中使用一个文件。 If i upload the file to Data/Data/APP/files then it is added with -rw-rw-rw permissions which i can then use in my application.如果我将文件上传到 Data/Data/APP/files,那么它会添加 -rw-rw-rw 权限,然后我可以在我的应用程序中使用它。 If i programatically write the file to getFilesDir() the exact same directory, i can see the 2 exact files in the same directory, however the programatically saved file has permissions -rw------- i cannot then access the file in my app using getfilesDir().如果我以编程方式将文件写入 getFilesDir() 完全相同的目录,我可以在同一目录中看到 2 个完全相同的文件,但是以编程方式保存的文件具有权限 -rw------- 然后我无法访问该文件我的应用程序使用 getfilesDir()。 this is how the file is saved:这是文件的保存方式:

    public void writeFileOnInternalStorage(Context mcoContext,String sFileName, String sBody){
        File file = new File(getApplicationContext().getFilesDir(), "");
        if(!file.exists()){
            file.mkdir();
        }

        try{
            File gpxfile = new File(file, sFileName);
            FileWriter writer = new FileWriter(gpxfile);
            writer.append(sBody);
            writer.flush();
            writer.close();

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

        }
    }

How can i get the correct permissions to use the file.我怎样才能获得使用该文件的正确权限。 It may well not be a permissions issue it maybe the way i am saving the file?这可能不是权限问题,可能是我保存文件的方式? It is a.graphml extension file.它是一个 .graphml 扩展文件。

What do you mean by cannot access your file?无法访问您的文件是什么意思?

Take a look at this documentation ( https://developer.android.com/training/data-storage ), you don't need permission to access (read and write) files inside App-Specific Directory.看看这个文档( https://developer.android.com/training/data-storage ),您不需要访问(读取和写入)应用程序特定目录中的文件的权限。


Edit:编辑:

I'm assuming that you already stored the file to the disk.我假设您已经将文件存储到磁盘上。

Please note that to read and write files inside App-Specific Directory doesn't require permission.请注意,在 App-Specific Directory 中读取和写入文件不需要权限。 You can read it using this simple code.您可以使用这个简单的代码阅读它。

public File readFile(Context context, String fileName) {
    File file = new File(context.getFilesDir(), fileName);
    // do other stuff, like checking if the file exist, etc.
    return file;
}

It doesn't matter what file extension it is, as long as the file exists, you will get it.不管它是什么文件扩展名,只要文件存在,你就会得到它。

Actually there are so many articles that already cover this topic, please take a look to understand this topic better.实际上有很多文章已经涵盖了这个主题,请看一下以更好地理解这个主题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM