安卓getExternalStorageDirectory()返回无效的目录 [英] Android getExternalStorageDirectory() returns invalid directory

查看:208
本文介绍了安卓getExternalStorageDirectory()返回无效的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想补充OBB文件的支持,我遇到了一个bizare情况。当我打电话 getExternalStorageDirectory(),它返回不我的设备上存在的路径。

I am trying to add OBB file support and I have run into a bizare situation. When I call getExternalStorageDirectory(), it returns a path that does not exist on my device.

我把这个在我的应用程序的启动:

I call this at the start of my app:

Log.i( LOG_ID, "XXXXXXXXXXXXXXXX <--- "+Environment.getExternalStorageDirectory().getAbsolutePath() );

它返回:

XXXXXXXXXXXXXXXX <--- /storage/emulated/0

但是,当我通过 ADB -d外壳连接及运行 LS -al /存储/模拟我得到:

lrwxrwxrwx root     root              2013-12-04 15:17 legacy -> /mnt/shell/emulated/0

正如你所看到的路径 getExternalStorageDirectory()返回是不是我的设备上有效。任何想法,为什么这将是我怎么能找到OBB文件,而无需硬编码到/ mnt / SD卡/

As you can see the path getExternalStorageDirectory() returned is not valid on my device. Any idea why this would be and how I can locate the OBB files without hard coding /mnt/sdcard/?

编辑:

我刚刚运行一些进一步的测试:

I have just run some further testing:

Works - stat( "/sdcard/Android/obb" );
Fails - stat( "/sdcard/Android/obb/uk.co.mycomp.myapp" );

shell@android:/mnt/shell $ ls -ald /sdcard/Android/obb/                        
drwxrwxr-x root     sdcard_rw          2013-12-18 16:42 
shell@android:/mnt/shell $ ls -ald  /sdcard/Android/obb/uk.co.mycomp.myapp
drwxrwxr-x root     sdcard_rw          2013-12-18 18:36

正如你可以看到这两个目录是相同的但我只能访问父。这可能会或可能不会有任何相关性我的问题...

As you can see the two directories are identical yet I can only access the parent. This may or may not have any relevance to my issue...

推荐答案

<打击>最近的Andr​​oid版本中,坐骑是不同的用户ID的实际上是不同的,你正在试图调查这种方式无效。

您尝试在调查的存储路径是由一个事实,即近期的Andr​​oid版本充分利用Linux内核的每个进程的命名空间功能,以提供不同组坐骑不同工艺的祖先感到沮丧。这很可能是连接到向支持多个(人)的用户帐户,与外部存储的既独特又共享子部分的努力。

Your attempt at investigating the storage paths is frustrated by the fact that recent Android versions leverage the "Per-process namespaces" feature of the Linux kernel to provide different sets of mounts to different process ancestries. This is likely connected to the efforts towards supporting multiple (human) user accounts, with both unique and shared sub-sections of External Storage.

当你运行一个亚行的shell,你会得到一组坐骑adbd继承。

When you run an adb shell, you get a set of mounts inherited from adbd.

但相比之下,code。通过一个应用程序的运行(无论是在它的过程中,还是一个子进程)得到了的不同的的设置坐骑,显然是从受精卵无论是继承或此后不久通过配置的流程分化code果然刚发芽受精卵进入你的应用程序的过程。

But in contrast, code run by an application (either in it's process, or a child process) gets a different set of mounts, apparently either inherited from zygote or configured soon thereafter by the process differentiation code which turns a freshly budded zygote into your app's process.

要得到一个什么挂载你的应用程序看到,安装类似 connectbot ,你可以用它来得到一个应用程序的本地外壳的后裔,并运行安装的想法命令,或感兴趣的任何其它的调查。例如:

To get an idea of what mounts your app sees, install something like connectbot which you can use to get a local shell descended from an application process, and run the mount command, or any other investigation of interest. For example:

u0_a99@build:/ $ mount

/dev/fuse /storage/emulated/0 fuse rw....
/dev/fuse /storage/emulated/legacy fuse rw.....

在比较,从亚行外壳检查:

In comparison, checking from the adb shell:

shell@build:/mnt $ mount

/dev/fuse /mnt/shell/emulated fuse rw.....


混淆,因为坐骑是由流程的确定的血统的,而不是它的用户ID 的,使用运行-as 工具亚行的不会让你同样的看法作为一个应用程序或者它的孩子,而是反而会给你亚行的观点。


Confusingly, because the mounts are determined by the processes's ancestry rather than it's user id, using the run-as tool from adb will not get you the same view as an application process or it's child, but rather instead will give you adb's view.

不过,如果你有一个正在运行的应用程序的进程的PID,您可以使用运行方式看,在它的 / proc /进程## /坐骑文件,而不不必实际获得应用程序的后裔壳。

However, if you have the pid of a running app's process, you can use run-as to look in it's /proc/pid##/mounts file, without having to actually get a shell descended from the app.

这篇关于安卓getExternalStorageDirectory()返回无效的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆