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

4

19

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

Calices answered 25/10, 2011 at 8:58 Comment(0)
T
22

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);
}
Thermionic answered 12/7, 2013 at 13:30 Comment(2)
Do we know if this is "official"? Would hate for it to just stop working after an update. Either way, it solved my problem, probably should accept the answer.Levity
It's part of the SDK since API Level 9, so any android device running Gingerbread or later it will work on. REF: developer.android.com/reference/java/io/File.htmlThermionic
W
9

OP asked how to give access to a file in the following hierarchy: appdir/files/subdir/myfile.
The answers provided here don't take subfolder into account, so I feel there's a room for improvement.

In order to access file in hierarchy, a consumer should have execute permission on each folder in a path in order to access (read, write, execute) files underneath it.


For API >= 24

Starting from API 24, Android restricts access to appdir (a.k.a /data/data/appdir):

In order to improve the security of private files, the private directory of apps targeting Android 7.0 or higher has restricted access (0700). This setting prevents leakage of metadata of private files, such as their size or existence.

The appdir doesn't have world-execute permission, and therefore you can't cd into it:

angler:/ $ cd /data/data
angler:/data/data $ cd com.myapp
/system/bin/sh: cd: /data/data/com.myapp: Permission denied

Bottom line: you can give world-readable permission to one of the files in your app's folder, but no other app (as long as they don't share the same Linux user ID) will be able to read them.

Not only that: attempt to pass a file:// URI to external app will trigger a FileUriExposedException.


For API < 24

The appdir folder has world-execute permission by default:

shell:/ $ cd /data/data
shell:/data/data $ cd com.myapp
shell:/data/data/com.myapp $

Note that even the appdir/files folder has world-execute permission:

shell:/data/data/com.myapp $ cd files
shell:/data/data/com.myapp/files $ 

But if you'll try to create a sub-folder (underneath files folder), using this code:

File subdir = new File(context.getFilesDir(), "subfolder");
subdir.mkdir();

it won't have world-execute permission:

shell:/ $ cd /data/data/com.myapp/files
shell:/data/data/com.myapp/files $ cd subfolder
/system/bin/sh: cd: /data/data/com.myapp/files/subfolder: Permission denied
shell:/data/data/com.myapp/files $ run-as com.myapp
shell:/data/data/com.myapp $ cd files
shell:/data/data/com.myapp/files $ ls -l 
total 72
drwx------ 3 u0_a226 u0_a226 4096 2016-11-06 11:49 subfolder
shell:/data/data/com.myapp/files $ 

Therefore, you have to explicitly give your newly created folder world-execute permission using File#setExecutable method (added in API 9):

subdir.setExecutable(true, false);

And only then, as suggested by others, you can create your file and give it world-readable permission:

File file = new File(subdir, "newfile");
if(!file.exists()) {
    file.createNewFile();
    file.setReadable(true, false);
}

Doing that will allow any external application read your file:

shell:/ $ cd /data/data/com.myapp/files
shell:/data/data/com.myapp/files $ cd subfolder
shell:/data/data/com.myapp/files/subfolder $ cat newfile > /sdcard/1
shell:/data/data/com.myapp/files/subfolder $ cd /sdcard        
shell:/sdcard $ ls 1 
1
Wilen answered 9/11, 2016 at 18:4 Comment(1)
Thanks for the great info! I have two questions by the way. 1. Does any custom subfolder under /data/data/com.myapp become private? Or folders under /data/data/com.myapp/files only as you wrote here? 2. Are you calling setReadable(true, false) because files are always created using MODE_PRIVATE mode by default? Or just calling to make sure?Smasher
E
6

If you are running it on a rooted device, change file permissions using:

Runtime.getRuntime().exec("chmod 777 " + PATH + fileName);

Or try File.setReadable(), File.setWritable while creating the file.

Elevation answered 29/7, 2012 at 5:18 Comment(1)
well thanks, but I need it for any device, which possibly is not rooted. I think it is a kind of bugCalices
S
0

One workaround I used in the past was to re-open the already existing file using openFileOutput in append mode, and pass in the world readable and/or world writable flags during that time. Then immediately close the file without writing to it.

I like the new methods added in API 9 better though.

Schexnayder answered 12/2, 2015 at 16:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.