在 Android Marshmallow 中遍历文件系统
Traversing the filesystem in Android Marshmallow
我的应用程序中有一个非常粗糙但有效的文件系统浏览器,它直到最近才找到(我假设直到我,更糟糕的是,我的用户更新到 Marshmallow)。
它的基本工作非常简单:我让用户从 SD 卡(我从 Environment.getExternalStorageDirectory()
获得)开始,然后在浏览器中显示相关文件和所有目录,添加人为地使用 ..
目录向上导航树,除非当前目录不是 /
。
正如我所说,它工作得很好并且有许多非常理想的特性:
- 它允许非常全面地导航文件系统:用户几乎可以访问所有内容,但有一些很小的例外(比如一些系统目录和
/data/data
目录)
- 很容易从 SD 卡转到
/
文件系统,然后返回(可能是不同的)SD 卡
- 它非常类似于用户已经习惯的浏览器(FileMangers、Finders、Midnight Commander,随便你怎么说...)
现在它不再起作用了,这不是运行时权限的问题(我已经检查了那部分)。
情况如下:
- 在我的 Nexus 5 上,存储位于
/storage/emulated/0
- 我可以很好地浏览存储子目录(我确实有权限),但是 我不能进入
/storage/emulated
,因此, 我无法访问 /
文件系统
- 根据“2”,我无法从一张 SD 卡转到另一张 (!!!)
从 /
开始也无济于事,因为 /storage/emulated
仍然无法从 /storage
列出(不是我期望的不同),因此我在根文件系统中隔离。
问题 1:有没有办法从这个 windows-like C:
/D:
/ E:
bulls..t Os 开发人员 误导性地 和 人为地 将我们归入,并且取回真正的 Android 与生俱来的(美丽的)POSIX 文件系统?
问题 2:如果没有,是否至少有一种方法可以获取所有 SD 卡位置的列表,以允许模拟以前的行为?
谢谢!
I let the user start from the SD card (which I got from Environment.getExternalStorageDirectory())
External storage is not removable storage 在大多数 Android 设备上。
then I show the relevant file and all directories in the browser, adding artificially a .. directory to navigate up the tree, unless the current directory isn't / already.
不要求您的应用可读取 getExternalStorageDirectory()
等方法返回的其中一个目录之上的目录。一般来说,POSIX 文件系统的用户可以证明,仅仅因为您可以访问一个目录并不意味着您可以访问父目录或对等目录。
I can navigate the storage sub-directories just fine (I do have the permissions), but I can't go up to /storage/emulated and, therefore, I cannot reach the / filesystem
这不是您的 Nexus 5 独有的,也不是 Android 6.0 独有的。你会 运行 到处都是这样的问题,因为你假设你对父目录有读取权限,而这个假设并不普遍有效。例如,在 Android 4.2+ 平板电脑和 Android 5.0+ 手机上,二级用户帐户对于 Environment.getExternalStorageDirectory()
等内容的路径将与您习惯的路径不同。
it follows from '2' that I cannot go from one SD card to another (!!!)
您无法在 Android 4.4 及更高版本上任意访问 removable storage。
Question 1: Is there a way to revert from this windows-like C:/D:/E: bulls..t the Os developers have misguidedly and artificially relegated us into, and get back the (beautiful) POSIX filesystem that is the true Android's birthright?
您可以对您的设备进行 root,然后安排派生具有超级用户权限的进程,这些进程具有对文件系统所有方面的读取权限。
否则,与其他 POSIX 文件系统一样,您无法访问所有内容。
Question 2: If not, is there at least a way to get a list of all SD card locations, in a way that would allow to emulate the previous behavior?
没有。首先,如前所述,由于 POSIX 文件系统权限,您之前的行为已经不可靠。其次,由于文件系统权限,您通常无权访问可移动存储。
当然欢迎您在 Context
上调用 getExternalFilesDirs()
(注意复数)之类的方法。如果设备具有可移动存储,则这些方法返回的第二个和后续 File
对象将指向您可以读写的可移动存储上的位置。但是,您将无法访问父目录或可移动存储上的其他随机位置。
我的应用程序中有一个非常粗糙但有效的文件系统浏览器,它直到最近才找到(我假设直到我,更糟糕的是,我的用户更新到 Marshmallow)。
它的基本工作非常简单:我让用户从 SD 卡(我从 Environment.getExternalStorageDirectory()
获得)开始,然后在浏览器中显示相关文件和所有目录,添加人为地使用 ..
目录向上导航树,除非当前目录不是 /
。
正如我所说,它工作得很好并且有许多非常理想的特性:
- 它允许非常全面地导航文件系统:用户几乎可以访问所有内容,但有一些很小的例外(比如一些系统目录和
/data/data
目录) - 很容易从 SD 卡转到
/
文件系统,然后返回(可能是不同的)SD 卡 - 它非常类似于用户已经习惯的浏览器(FileMangers、Finders、Midnight Commander,随便你怎么说...)
现在它不再起作用了,这不是运行时权限的问题(我已经检查了那部分)。
情况如下:
- 在我的 Nexus 5 上,存储位于
/storage/emulated/0
- 我可以很好地浏览存储子目录(我确实有权限),但是 我不能进入
/storage/emulated
,因此, 我无法访问/
文件系统 - 根据“2”,我无法从一张 SD 卡转到另一张 (!!!)
从 /
开始也无济于事,因为 /storage/emulated
仍然无法从 /storage
列出(不是我期望的不同),因此我在根文件系统中隔离。
问题 1:有没有办法从这个 windows-like C:
/D:
/ E:
bulls..t Os 开发人员 误导性地 和 人为地 将我们归入,并且取回真正的 Android 与生俱来的(美丽的)POSIX 文件系统?
问题 2:如果没有,是否至少有一种方法可以获取所有 SD 卡位置的列表,以允许模拟以前的行为?
谢谢!
I let the user start from the SD card (which I got from Environment.getExternalStorageDirectory())
External storage is not removable storage 在大多数 Android 设备上。
then I show the relevant file and all directories in the browser, adding artificially a .. directory to navigate up the tree, unless the current directory isn't / already.
不要求您的应用可读取 getExternalStorageDirectory()
等方法返回的其中一个目录之上的目录。一般来说,POSIX 文件系统的用户可以证明,仅仅因为您可以访问一个目录并不意味着您可以访问父目录或对等目录。
I can navigate the storage sub-directories just fine (I do have the permissions), but I can't go up to /storage/emulated and, therefore, I cannot reach the / filesystem
这不是您的 Nexus 5 独有的,也不是 Android 6.0 独有的。你会 运行 到处都是这样的问题,因为你假设你对父目录有读取权限,而这个假设并不普遍有效。例如,在 Android 4.2+ 平板电脑和 Android 5.0+ 手机上,二级用户帐户对于 Environment.getExternalStorageDirectory()
等内容的路径将与您习惯的路径不同。
it follows from '2' that I cannot go from one SD card to another (!!!)
您无法在 Android 4.4 及更高版本上任意访问 removable storage。
Question 1: Is there a way to revert from this windows-like C:/D:/E: bulls..t the Os developers have misguidedly and artificially relegated us into, and get back the (beautiful) POSIX filesystem that is the true Android's birthright?
您可以对您的设备进行 root,然后安排派生具有超级用户权限的进程,这些进程具有对文件系统所有方面的读取权限。
否则,与其他 POSIX 文件系统一样,您无法访问所有内容。
Question 2: If not, is there at least a way to get a list of all SD card locations, in a way that would allow to emulate the previous behavior?
没有。首先,如前所述,由于 POSIX 文件系统权限,您之前的行为已经不可靠。其次,由于文件系统权限,您通常无权访问可移动存储。
当然欢迎您在 Context
上调用 getExternalFilesDirs()
(注意复数)之类的方法。如果设备具有可移动存储,则这些方法返回的第二个和后续 File
对象将指向您可以读写的可移动存储上的位置。但是,您将无法访问父目录或可移动存储上的其他随机位置。