How to get file path using file descriptor on Android?

我是研究僧i 提交于 2019-12-06 01:03:43

问题


I need to get file absolute path using file descriptor which was returned by getFileDescriptor(). How can i do it?


回答1:


On a system running under a Linux kernel, which would be all current Android implementations, each file descriptor will have an entry in /proc/[processid]/fd which is a symbolic link to the target of the file descriptor - not only for regular files, but also for many other possible targets such as pipes and sockets.

Here's a partial example for a cat > /mnt/sdcard/foo process running on an android device

$ ls -l /proc/3528/fd
lrwx------ shell    shell             2013-06-06 10:31 0 -> /dev/pts/1
l-wx------ shell    shell             2013-06-06 10:31 1 -> /mnt/sdcard/foo
lrwx------ shell    shell             2013-06-06 10:31 2 -> /dev/pts/1
lrwx------ shell    shell             2013-06-06 10:31 6 -> socket:[188850]

Reading symbolic links in Java "is left as an exercise to the reader".

On a desktop linux, there's the lsof tool which trolls through these directories for all processes and dumps out a list of open files - however, that would not be too useful on Android where the usage of per-app userids would restrict such trolling. But you can still lookup the fd's belonging to your app itself, or any other process running under the same userid.



来源:https://stackoverflow.com/questions/16937708/how-to-get-file-path-using-file-descriptor-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!