Android File Permissions issues on FilesDir()

1.9k Views Asked by At

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. 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(). 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.

1

There are 1 best solutions below

5
On

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.


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. 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.