Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get file path using file descriptor on Android?

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

like image 560
4ntoine Avatar asked Jun 05 '13 10:06

4ntoine


1 Answers

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.

like image 163
Chris Stratton Avatar answered Oct 23 '22 13:10

Chris Stratton