0

i've create this method to read a file:

@TargetApi(19)
public String getContentFile(String path) throws FileNotFoundException {

    BufferedReader br = new BufferedReader(new FileReader(path));

    try {
        Toast.makeText(MainActivity.this, "entrato", 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";

    }

and i call it in MainActivity, this code work for file in storage folder but not for file in data/data folder, how can i change this? Tha app is for root device, my question is: how to access in other folders? the exception say: open failed: EACCES (Permission denied) so i've change the file permission and it works but how to change permission temporarily?

michele buzzoni
  • 97
  • 1
  • 3
  • 9
  • Please log your `Exception` to LogCat, using `Log.e()`, then use the LogCat tool to examine the Java stack trace associated with your error: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Mar 22 '15 at 17:20
  • Are you reading from your app's sub directory of data/data or someone else's? Or from the root of data/data...? – JHH Mar 22 '15 at 17:42
  • the exception is FileNotFound, but the file is present, i'm reading from root, the path example is "/data/data/somefolder/file.xml", but if i use "/storage/sdcard/somefile" this code works! – michele buzzoni Mar 22 '15 at 17:50
  • the exception say: open failed: EACCES (Permission denied) – michele buzzoni Mar 23 '15 at 09:02

1 Answers1

1

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.

Community
  • 1
  • 1
JHH
  • 8,567
  • 8
  • 47
  • 91