Android: disambiguating file paths

前端 未结 4 2135
北海茫月
北海茫月 2021-02-20 15:03

In my app, users pick files. Internally, I store information about the file, which I key based on the file path. Next time that file is used, I do stuff with the stored inform

4条回答
  •  天涯浪人
    2021-02-20 15:35

    On newer Android versions, SD storage is available from many paths, for example:

    /storage/emulated/0
    /storage/emulated/legacy (root account most of the time)
    /sdcard
    /data/media
    

    If you check on which device those paths are located, some are on different devices (because of fuse 'virtual' filesystem), hence getting their canonical path doesn't lead to the same file path, even though they are actually the same files.

    Furthermore, it appears that on Marshmallow, things get worse and even file path under /sys (full of redirections/links) are not reported properly and getCanonicalPath() returns the original path instead of actual canonical path.

    While a ls -l (or readlink) on the given file path will show the actual canonical path, the API doesn't work anymore. Unfortunately running a readlink or ls -l is very slow (average of 130ms when the shell is already running), compared to an already slow, but much faster getCanonicalPath(), it's a pity.

    Conclusion, getCanonicalPath is broken and has always been broken.

提交回复
热议问题