Search code examples
androidfilefile-permissions

Android File permission


I have used class File in Android. The method File.setReadable(boolean readable, boolean ownerOnly), but it failed :( The document of Android developers wrote:

"true if and only if the operation succeeded. The operation will fail if the user does not have permission to change the access permissions of this abstract pathname. If readable is false and the underlying file system does not implement a read permission, then the operation will fail."

So, what is "permission to change the access permissions of this abstract pathname"? Hope everybody help please.


Solution

  • what is "permission to change the access permissions of this abstract pathname"?

    On whatever desktop OS you happen to be using — Windows, OS X/macOS, or Linux — there are files that you can change permissions on, and files that you cannot. This is part of filesystem security models offered by the OS. For example, on a Linux server, the Linux user account associated with the Web server cannot change permissions on arbitrary files. This way, if that Web server has a bug (e.g., buffer overrun) that can be exploited, it limits how much damage the exploit can do.

    Android, being based on Linux, inherits these capabilities. It uses them to help maintain privacy between apps. App A cannot access App B's files on internal storage, and neither app can access arbitrary files on removable storage (on Android 4.4+).

    In general, on Android, if you are trying to change the permission of a file, "you're doing it wrong". Whatever problem you are trying to solve is better served being solved in some other way, such as:

    • Putting the file somewhere that is publicly readable in the first place (e.g., external storage), or

    • Using FileProvider to make a private file accessible by other apps as needed