How Android obtains the system external memory card path

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.