10

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

4ntoine
  • 19,816
  • 21
  • 96
  • 220
  • Nice idea for investigating those USB device files! If you build a debug apk, you can use adb shell 'run-as your.package.name' to get a shell as the app's userid and then while it is running use ls -l from that shell on the app process's /proc entries to examine the fd links, rather than having to figure out how to do so from java. – Chris Stratton Jun 06 '13 at 14:45
  • Chris can you explain more in detail , i also trying get USB OTG path from java code but i am unable to fine it's actual path as google has lot's of restriction for removable device. – Pranav Mar 31 '19 at 10:00

1 Answers1

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.

Chris Stratton
  • 39,853
  • 6
  • 84
  • 117