How to get file path using file descriptor on Android?

前端 未结 1 1851
一生所求
一生所求 2021-02-20 17:33

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

1条回答
  •  时光说笑
    2021-02-20 17:57

    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.

    0 讨论(0)
提交回复
热议问题