The Android system can get the path of the memory card through Environment.getexternalstoragedirectory (), but now there are a lot of phones built in a storage space, but also support the external SD card insertion, This through the Environment.getexternalstoragedirectory () method to obtain is the location of the internal memory card, the need to obtain the path of the external memory card is more troublesome, here to learn from the online code, slightly modified, in the existing mobile phone to do the test, The effect can also, of course, there are some other wonderful models are not covered.
Package Com.example.getpath;import Java.io.bufferedreader;import Java.io.file;import java.io.inputstream;import Java.io.inputstreamreader;import Android.annotation.suppresslint;import Android.app.activity;import Android.os.bundle;import Android.os.environment;import Android.util.log;public class MainActivity extends Activity {@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); GetPath2 ();} @SuppressLint ("Sdcardpath") public String GetPath2 () {string sdcard_path = null; String Sd_default = Environment.getexternalstoragedirectory (). GetAbsolutePath (); LOG.D ("text", Sd_default), if (Sd_default.endswith ("/")) {Sd_default = sd_default.substring (0, Sd_default.length ()-1) ;} Get the path try {Runtime runtime = Runtime.getruntime (); Process proc = runtime.exec ("mount"); InputStream is = Proc.getinputstream (); InputStreamReader ISR = new InputStreamReader (IS); String Line; BufferedReader br = new BufferedReader (ISR);= Br.readline ()) = null) {if (Line.contains ("secure")) Continue;if (Line.contains ("ASEC")) Continue;if (Line.contains ( "Fat") && line.contains ("/mnt/")) {String columns[] = Line.split (""); if (columns! = NULL && columns.lengt H > 1) {if (Sd_default.trim (). Equals (Columns[1].trim ())) {continue;} Sdcard_path = Columns[1];}} else if (line.contains ("fuse") && line.contains ("/mnt/")) {String columns[] = Line.split (""); if (columns! = NULL && columns.length > 1) {if (Sd_default.trim (). Equals (Columns[1].trim ())) {continue;} Sdcard_path = Columns[1];}}} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();} LOG.D ("text", Sdcard_path); return sdcard_path;}}
How Android obtains the system external memory card path