Find location of a removable SD card

前端 未结 22 2615
南方客
南方客 2020-11-21 11:32

Is there an universal way to find the location of an external SD card?

Please, do not be confused with External Storage.

Environment.getExternalStorage

22条回答
  •  一向
    一向 (楼主)
    2020-11-21 11:42

    Here is the way I use to find the external card. Use mount cmd return then parse the vfat part.

    String s = "";
    try {
    Process process = new ProcessBuilder().command("mount")
            .redirectErrorStream(true).start();
    
    process.waitFor();
    
    InputStream is = process.getInputStream();
    byte[] buffer = new byte[1024];
    while (is.read(buffer) != -1) {
        s = s + new String(buffer);
    }
    is.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    
    //用行分隔mount列表
    String[] lines = s.split("\n");
    for(int i=0; i

提交回复
热议问题