Android 文件权限

Android File permission

我在Android中使用了class个文件。 File.setReadable(boolean readable, boolean ownerOnly) 方法失败了:( Android 开发者的文档写道:

"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."

那么,什么是"permission to change the access permissions of this abstract pathname"? 希望大家帮帮忙

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

在您碰巧使用的任何桌面 OS 上 — Windows、OS X/macOS 或 Linux — 有您可以更改的文件的权限和您不能访问的文件。这是 OS 提供的文件系统安全模型的一部分。例如,在 Linux 服务器上,与 Web 服务器关联的 Linux 用户帐户不能更改对任意文件的权限。这样,如果该 Web 服务器有一个可以被利用的错误(例如,缓冲区溢出),它会限制该漏洞可以造成的破坏程度。

Android,基于Linux,继承了这些能力。它使用它们来帮助维护应用程序之间的隐私。应用程序 A 无法在 internal storage, and neither app can access arbitrary files on removable storage(在 Android 4.4+)上访问应用程序 B 的文件。

一般来说,在 Android,如果您试图更改文件的权限,"you're doing it wrong"。无论您试图解决什么问题,最好以其他方式解决,例如:

  • 首先将文件放在公开可读的地方(例如,external storage),或者

  • 使用FileProvider使其他应用程序可以根据需要访问私人文件