How to find the path of USB which is mounted to android?
Asked Answered
A

1

4

I am working on an android application to read data from USB. The usb can be connected to android with serial port and my app can find it.

Now, I want to read data files and folder from USB. I have read many articles. I found that they use this code :

Environment.getExternalStorageDirectory();

However in my case, I got that the path is /storage/emulated/0. When I try to read all the files which are contained in the path, I got the following statements:

/storage/emulated/0/Android
/storage/emulated/0/Music
/storage/emulated/0/Podcasts
/storage/emulated/0/Ringtones

and etc.

but the path of my usb is not found. So, I'm not sure is it the correct way to read files from USB?

Here is my code :

File f = Environment.getExternalStorageDirectory();
File[] files = f.listFiles();
String fol = "";
for (File inFile : files) {
    if (inFile.isDirectory()) {
        fol += inFile.toString()+"\n";
    }
}
TextView tv = (TextView) findViewById(R.id.demoTitle);
tv.setText(fol);
Almandine answered 28/4, 2013 at 12:24 Comment(0)
S
3

get all mounted device by using this code

public String getStoragepath() {
    try {
        Runtime runtime = Runtime.getRuntime();
        Process proc = runtime.exec("mount");
        InputStream is = proc.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        String line;
        String[] patharray = new String[10];
        int i = 0;
        int available = 0;

        BufferedReader br = new BufferedReader(isr);
        while ((line = br.readLine()) != null) {
            String mount = new String();
            if (line.contains("secure"))
                continue;
            if (line.contains("asec"))
                continue;

            if (line.contains("fat")) {// TF card
                String columns[] = line.split(" ");
                if (columns != null && columns.length > 1) {
                    mount = mount.concat(columns[1] + "/requiredfiles");

                    patharray[i] = mount;
                    i++;

                    // check directory is exist or not
                    File dir = new File(mount);
                    if (dir.exists() && dir.isDirectory()) {
                        // do something here

                        available = 1;
                        finalpath = mount;
                        break;
                    } else {

                    }
                }
            }
        }
        if (available == 1) {

        } else if (available == 0) {
            finalpath = patharray[0];
        }

    } catch (Exception e) {

    }
    return finalpath;
}
Septum answered 13/6, 2013 at 10:47 Comment(2)
Thanks for the code, i use it as fallback, i first check if a file "proc/mounts" is existing and i read it, if not then i read it via this script.Nils
We should not relly on "fat" or "vfat" new devices sometimes has "fuse" as partition type, its big script detecting partition, but they starts with "/dev/" and may be vfat or fuse, and you may have duplicate paths to same partition, o beware of that.Nils

© 2022 - 2024 — McMap. All rights reserved.