ikbal ikbal - 10 months ago 78
Android Question

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

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);
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

Ne0 Ne0
Answer Source

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.setReadable(true, false);