Get the Android SDK or mobile directory path
Google provides us with an API to get the SDK or mobile directory path:
1. Get the SD card directory
File file1 = Environment.getexternalstoragedirectory ();
2. Get the file directory of your phone's internal storage space
File file2 = Getfilesdir ();
3. Get the cache directory of internal storage space
File file3 = Getcachedir ();
4. Check if SD is mounted
String state = Environment.getexternalstoragestate ();
If state== "mounted" means to be mounted
Code:
Com.example.readwrite.MainActivity
1 PackageCom.example.readwrite;2 3 ImportJava.io.File;4 ImportJava.io.FileInputStream;5 ImportJava.io.FileOutputStream;6 Importjava.io.IOException;7 8 Importandroid.app.Activity;9 ImportAndroid.os.Bundle;Ten Importandroid.os.Environment; One ImportAndroid.util.Log; A - /** - * Forward slash represents the root directory two of the most common data storage methods the * - * One, memory two, local 1. Phone internal storage 2. External storage device (SD card) - * */ - Public classMainactivityextendsActivity { + - @Override + protected voidonCreate (Bundle savedinstancestate) { A Super. OnCreate (savedinstancestate); at Setcontentview (r.layout.activity_main); - //Existsdcard (); - //write (); - Listpath (); - //read (); - } in - Private voidwrite () { to ///mnt/sdcard +File File =environment.getexternalstoragedirectory (); -FileOutputStream out =NULL; the Try { *out =NewFileOutputStream (File.getpath () + "/fanfan.txt"); $ //out = new FileOutputStream (Panax Notoginseng //"/data/data/com.example.readwrite/fanfan.txt"); -Out.write ("12345". GetBytes ()); the}Catch(IOException e) { + e.printstacktrace (); A}finally { the if(Out! =NULL) { + Try { - out.close (); $}Catch(IOException e) { $ //TODO auto-generated Catch block - e.printstacktrace (); - } the } - }Wuyi } the - Private voidRead () { WuFileInputStream in =NULL; - Try { About //In = new FileInputStream ("/mnt/sdcard/fanfan.txt"); $in =NewFileInputStream ( -"/data/data/com.jiguang.test/databases/rep.db"); - byte[] bytes =New byte[2014]; - intLen =in.read (bytes); AString str =NewString (Bytes, 0, Len); +LOG.D ("Fanfan", "---------" +str); the}Catch(IOException e) { -LOG.D ("Fanfan", "error" +e.tostring ()); $}finally { the if(In! =NULL) { the Try { the in.close (); the}Catch(IOException e) { - e.printstacktrace (); in } the } the } About } the the /** the * Check to see if the SD card is mounted + * */ - Private voidExistsdcard () { the //get the status of an SD cardBayiString State =environment.getexternalstoragestate (); the the if(Environment.MEDIA_MOUNTED.equals (state)) { -LOG.D ("Fanfan", "SD card"); -}Else { theLOG.D ("Fanfan", "No SD card"); the } the } the - /** the * Get path through API the * */ the Private voidListpath () {94 //get the SD card directory theFile file1 =environment.getexternalstoragedirectory (); theLOG.D ("Fanfan", "SD card----" +File1.getpath ()); the //get the file directory for your phone's internal storage space98File file2 =Getfilesdir (); AboutLOG.D ("Fanfan", "Internal Store file----" +File2.getpath ()); - //gets the cache directory for the internal storage space101File File3 =Getcachedir ();102LOG.D ("Fanfan", "Internal storage cache Directory----" +File3.getpath ());103 }104}Get Path
1 /**2 * Check to see if the SD card is mounted3 * */4 Private voidExistsdcard () {5 //get the status of an SD card6String State =environment.getexternalstoragestate ();7 8 if(Environment.MEDIA_MOUNTED.equals (state)) {9LOG.D ("Fanfan", "SD card");Ten}Else { OneLOG.D ("Fanfan", "No SD card"); A } -}Check if the SD card is mounted
1 /**2 * Get path through API3 * */4 Private voidListpath () {5 //get the SD card directory6File file1 =environment.getexternalstoragedirectory ();7LOG.D ("Fanfan", "SD card----" +File1.getpath ());8 //get the file directory for your phone's internal storage space9File file2 =Getfilesdir ();TenLOG.D ("Fanfan", "Internal Store file----" +File2.getpath ()); One //gets the cache directory for the internal storage space AFile File3 =Getcachedir (); -LOG.D ("Fanfan", "Internal storage cache Directory----" +File3.getpath ()); -}getting paths through the API
Get the Android SDK or mobile directory path