how to create a file with world readable permission under subdirectory of files directory

ikbal picture ikbal · Oct 25, 2011 · Viewed 13.3k times · Source

I need to create files under myapp/files/subdir with global permission in my application. I do this because I use external applications to open some files Using this

 FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_WORLD_READABLE);

creates file only under files folder. Using

    File dir=new File(Constants.TASK_DIRECTORY);
    dir.mkdirs();
    File file=new File(dir, FILENAME);         
    file.createNewFile(); FileOutputStream fos=new FileOutputStream(file);

creates files under subdirectories but with private permissions. I need to find a way to compose those both to create a file in a subdirectory to be world readable

I have been trying a lot of things but none helped me and this was the longest time unanswered question of mine

Answer

Ne0 picture Ne0 · Jul 12, 2013

I know this is an old question, but here is the correct way

public void makeFileWorldReadable(String path)
{
    File f = new File(path);

    if(!f.exists()) // Create the file if it does not exist.
        f.createNewFile();
    f.setReadable(true, false);
}