Search code examples
androidpermissionsroot

How to access files in the data/data folder in Android, specifically for root devices, and handle "Permission denied" errors?


I've created a method to read a file in Android:

@TargetApi(19)
public String getContentFile(String path) throws FileNotFoundException {
    BufferedReader br = new BufferedReader(new FileReader(path));

    try {
        Toast.makeText(MainActivity.this, "Entered", Toast.LENGTH_SHORT).show();
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();

        while (line != null) {
            sb.append(line);
            sb.append(System.lineSeparator());
            line = br.readLine();
        }

        String everything = sb.toString();
        br.close();
        return everything;
    } catch (Exception e) {
        return "error";
    }
}

This code works for files in the storage folder but not for files in the data/data folder. My app is designed for rooted devices, and I'm encountering the "open failed: EACCES (Permission denied)" exception.

I've managed to make it work by changing the file permissions, but I'd like to know how to change permissions temporarily. Can someone guide me on how to access files in other folders and handle permission-related issues, especially on rooted devices?

Your assistance is much appreciated. Thank you!


Solution

  • Your app only has file access to your own directory, ie /data/data/your.package.name.

    EDIT: It is now evident that the OP is trying to make an app that accesses files outside of its own data directory on a rooted device. This makes this an entirely different question. As far as I know, it's not possible to directly make an app run as root. What you can do is to run su as a shell command, followed by whatever shell commands you need to run as root. There are several posts about how to do this, for example ANDROID: How to gain root access in an Android application?

    This means you might have to perform all your root access operations using shell commands rather than using normal Android API:s. However, if all you need to do is access a certain file outside your own data directory, perhaps you could accomplish this by using chown, chmod etc in a rooted shell to change the permission of the file you are trying to read so that your app's UID can access it (every app is automatically assigned a unique uid such as "u0_a42"). Then you should be able to access it using normal Java file operations.

    Perhaps you don't even need to actually change the owning user of the file to your app UID, but could use some existing group that your app already belongs to, such as the one controlling access to the sd card (this is speculation). You might want to give this a try:

    su
    chown root:sdcard_r /data/path/to/file
    

    Provided that your app holds android.permission.READ_EXTERNAL_STORAGE I reckon you should now be able to access the file. You could probably even use some other group that isn't mapped to any permission.