Recently due to project requirements, need to find files on the memory card, the test found that some of the mobile phone mount Path can not find, here to share an effective method.
/** * Get all memory card mount paths * @return */public static list<string> Getmountpathlist () {list<string> Pathli st = new Arraylist<string> (); Final String cmd = "cat/proc/mounts"; Runtime run = Runtime.getruntime ();//Get current JVM Runtime environment try {Process p = run.exec (cmd);//execute command Bufferedinputstream InputStream = new Bufferedinputstream (P.getinputstream ()); BufferedReader BufferedReader = new BufferedReader (new InputStreamReader (InputStream)); String Line; while (line = Bufferedreader.readline ()) = null) {//Get command after execution in console output information logger.i (line); Output information content:/data/media/storage/emulated/0 Sdcardfs rw,nosuid,nodev,relatime,uid=1023,gid=1023 0 0 string[] temp = textutils.split (line, ""); The analysis shows that the second space is followed by a path String result = Temp[1]; File File = new file (result); Type is directory, readable, writable, even if it is a mount path if (file.isdirectory () && file.canread () && file.canwrite ()) { LOGGER.D ("Add--" +file.getabsolutepath ()); Pathlist.add (result); }//Check whether the command failed to execute if (p.waitfor ()! = 0 && p.exitvalue () = = 1) { P.exitvalue () ==0 indicates normal end, 1: Abnormal End logger.e ("command execution failed!"); }} bufferedreader.close (); Inputstream.close (); } catch (Exception e) {LOGGER.E (e.tostring ()); command to execute the exception, add the default path Pathlist.add (Environment.getexternalstoragedirectory (). GetAbsolutePath ()); } return pathList; }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android get all memory card mount paths