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

后端 未结 1 1721
清歌不尽
清歌不尽 2020-12-21 04:44

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

相关标签:
1条回答
  • 2020-12-21 05:37

    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;
    }
    
    0 讨论(0)
提交回复
热议问题