How can i read file from data folder?

2019-07-18 12:17发布

问题:

In my app I must read data from a file in data folder. But I can not do that. I searched about that but many users say you can not read data from data folder. And we see that Rootbrowser can read data from android private folder files. My question is how can I read files from data/misc folder?

回答1:

In my app I must read data from a file in data folder

If by "data folder", you mean /data or arbitrary directories under it, you do not have read access to most of that, except perhaps on a rooted device.

I searched about that but many users say you can not read data from data folder

Correct.

And we see that Rootbrowser can read data from android private folder files

If by "Rootbrowser" you mean Root Browser, that app can only do this on rooted devices. You can tell that by reading the product description, where it says:

Root Browser is the ultimate file manager for rooted users



回答2:

There is no solution for reading any file from any folder (unless your device is rooted, and even then it may be device-dependent), but most likely you problem requires something more specific than that. For example, if one application wants to pass data to another one, it is possible. If you need to read a file created by your own application, it's no problem at all. Please ask a question about your particular problem rather than about the general case.

UPDATE: from the discussion in the comments it turns out that you use a rooted device and the permissions on the folder of interest do not allow everyone to read it, which is a normal decision about access policy. If it's ok for your app to be run only on rooted phones, you may borrow sudoForResult() from https://stackoverflow.com/a/26654728/755804 .

The difference between Runtime.getRuntime().exec("ls"); and sudoForResult("ls "+path) is that in the latter case ls is executed by su (invoked via Runtime.getRuntime().exec("su"); within sudoForResult()) and therefore has superuser permissions.

Alternatively, you may try to open access to these folders for everyone. This will require su, will have to be done from your program, and will be difficult to test because after the 1st run the directory permissions will be already changed. OTOH, if you manage to make the directory readable for your application via access permissions, you can unroot the device and still have your application working.