Android: disambiguating file paths

前端 未结 4 2134
北海茫月
北海茫月 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:32

    There is probably no easy solution for this. The problem is that apparently there are two different mount points in the file system that actually point to the same location. File.getCanonicalPath() can only resolve symbolic links but not different mount points.

    For example on my Nexus 4 this code:

    File file1 = new File(Environment.getExternalStorageDirectory() + "/Android");
    System.out.println("file 1: " + file1.getCanonicalPath());
    File file2 = new File("/sdcard/Android");
    System.out.println("file 2: " + file2.getCanonicalPath());
    

    prints

    file 1: /storage/emulated/0/Android
    file 2: /storage/emulated/legacy/Android
    

    I used this code to exec "mount" and print the output:

    Process exec = Runtime.getRuntime().exec("mount");
    InputStream in = exec.getInputStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    while (true) {
        String line = br.readLine();
        if (line == null)
            break;
        System.out.println(line);
    }
    in.close();
    

    The relevant output is:

    /dev/fuse /storage/emulated/0 fuse rw,nosuid,nodev,relatime,user_id=1023,group_id=1023,default_permissions,allow_other 0 0
    /dev/fuse /storage/emulated/legacy fuse rw,nosuid,nodev,relatime,user_id=1023,group_id=1023,default_permissions,allow_other 0 0
    

提交回复
热议问题