java Android 中的文件权限
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/571783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
File permissions in Android
提问by
I'm here just to ask something maybe very simple, I'm working with Files, FileOutputStreamand FileInputStream, But I just want to get/set a few props from the file, I mean, the owner the file, the permissions read/write, etc.
我在这里只是问一些可能很简单的问题,我正在使用 Files、FileOutputStream和FileInputStream,但我只想从文件中获取/设置一些道具,我的意思是,文件的所有者,权限读取/写等
Looking around I found the classes FileDescriptorand FilePermissionbut I don't have an idea of which I can use them, so I asking for some help about this; Actually I'm using the method setReadOnly()from the class Filebut that's now what I'm looking for.
环顾四周,我找到了FileDescriptor和FilePermission类,但我不知道我可以使用它们,所以我寻求一些帮助;实际上,我正在使用类File中的setReadOnly()方法,但这正是我现在要寻找的。
回答by Will
There are a couple of issues here:
这里有几个问题:
- An application, by default, has unrestricted access to its own files (read/write/delete/'execute'...whatever execute means) but no access to any other application's files.
- Android make it difficult to interact with other applications and their data except through Intents. Intents won't work for permissions because you're dependent on the application receiving the Intent to do/provide what you want; they probably weren't designed to tell anybody their files' permissions. There are ways around it, but only when the applications are desgned to operate in the same JVM (i.e. applications you have designed to operate in the same JVM together)
- 默认情况下,应用程序可以不受限制地访问自己的文件(读/写/删除/'执行'...无论执行意味着什么),但不能访问任何其他应用程序的文件。
- 除了通过 Intent 之外,Android 很难与其他应用程序及其数据进行交互。Intent 对权限不起作用,因为您依赖于接收 Intent 的应用程序来执行/提供您想要的;他们可能不是为了告诉任何人他们的文件权限。有很多方法可以解决它,但只有当应用程序被设计为在同一个 JVM 中运行时(即您设计的应用程序在同一个 JVM 中一起运行)
Your application can get and set permissions for its own filesbut cannot do that for anyone elses. To my knowledge there is no explicit concept of ownership exposed in the SDK, so you cannot find the owner of a file.
您的应用程序可以为自己的文件获取和设置权限,但不能为其他任何人执行此操作。据我所知,SDK 中没有明确的所有权概念,因此您无法找到文件的所有者。
Android is based on the Linux kernel, but it's not Linux. It's been heavily optimized for running on mobile devices. If you have a machine where an application can only play in its own sandbox you can cut out things like permissions and ownership and get a smaller, faster operating system. The only permissions your files have are the one's you place (and enforce) on them. Other applications' files do not exist.
Android 基于 Linux 内核,但它不是 Linux。它已针对在移动设备上运行进行了大量优化。如果您的机器上的应用程序只能在其自己的沙箱中运行,您可以删除权限和所有权等内容,并获得更小、更快的操作系统。您的文件拥有的唯一权限是您对其放置(并强制执行)的权限。其他应用程序的文件不存在。
回答by Tamas Czinege
Android uses the standard Java API for handling files and file permissions. Here's a a page discussing permissions in Java.
Android 使用标准的 Java API 来处理文件和文件权限。这是讨论 Java 权限的页面。
Basically all you need to do is getting the FilePermissionobject of the file, then getting the PermissionCollectionfrom the FilePermission object with the newPermissionCollection()method and then add or remove items to or from that collection.
基本上,您需要做的就是获取文件的FilePermission对象,然后使用newPermissionCollection()方法从 FilePermission 对象获取PermissionCollection,然后在该集合中添加或删除项目。
回答by matt
Well...if you only want to SEE the permissions couldn't you just use "ls -l" ?
好吧...如果您只想查看权限,您不能只使用“ls -l”吗?

