1. ROM Partition
In Android, the protection of data is very tight. In addition to the data that is placed on the SD card, the database, files, and other content owned by an application are not allowed to be accessed directly by other applications, and this part of the data is in/data/data.
The SD card referred to here is the logical SD card, for example, I now use the Galaxy S4 ROM is the 16g,android system will divide the 16g into two parts, one is the system and application data area, the other is a virtual SD card, can be called as the internal SD card.
We usually inserted into the mobile phone is TF card, referred to as Micro SD card, the specific can refer to Baidu.
The default built-in SD card mount point for Samsung phones is/storage/emulated/0.
Android is Linux-based, Linux in order to use the storage device will be mounted to the Linux directory tree (Root), Android is generally mounted to the directory corresponding to the/storage/emulated/directory or/ Storage in the corresponding directory, if you can put two memory cards, those may be mounted separately to/storage/emulated/0/and/storage/emulated/1/such directories. And you get/storage/emulated/0/is the real directory of your SD card. is not a reference path. What you might get in the past is a path like/sdcard. That's a link (the reference you're referring to), linked to your SD upright mount directory/storage/emulated/0. So when you access two paths, you get the same content.
2. Get the internal and external SD card path
Test environment Samsung Note3
Require permissions
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE" ></uses-permission>
ImportJava.io.BufferedReader;ImportJava.io.File;ImportJava.io.InputStream;ImportJava.io.InputStreamReader;Importjava.util.ArrayList;Importjava.util.List;ImportAndroid.os.Bundle;Importandroid.os.Environment;Importandroid.app.Activity;ImportAndroid.view.Menu; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); StringBuilder Log=NewStringBuilder (); String Inpath=Getinnersdcardpath (); Log.append ("Built-in SD card path:" + Inpath + "\ r \ n"); List<string> extpaths =Getextsdcardpath (); for(String path:extpaths) {log.append ("External SD card path:" + path + "\ r \ n"); } System.out.println (Log.tostring ()); } /*** Get the built-in SD card path *@return */ PublicString Getinnersdcardpath() {return environment.getexternalstoragedirectory(). GetPath (); } /*** Get the external SD card path *@returnshould be a record or empty*/ PublicList<string> Getextsdcardpath() {List<string> LResult =NewArraylist<string>(); Try{Runtime rt=Runtime.getruntime (); Process proc= Rt.exec ("Mount"); InputStream is=Proc.getinputstream (); InputStreamReader ISR=NewInputStreamReader (IS); BufferedReader BR=NewBufferedReader (ISR); String Line; while(line = Br.readline ())! =NULL) { if(Line.contains ("Extsdcard") {String [] arr= Line.split (""); String Path= Arr[1]; File File=NewFile (path); if(File.isdirectory ()) {lresult.add (path); }}} isr.close (); } Catch(Exception e) {}returnLResult; } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.activity_main, menu); return true; } }
Printing results:
1. After inserting an external SD card
内置SD卡路径:/storage/emulated/0外置SD卡路径:/storage/extSdCard
2. After removing the external SD card
内置SD卡路径:/storage/emulated/0
SDcard:
in today's Android phone, because there are built-in storage from ROM and can plug in SD card two control, so it produced/mnt/sdcard and/mnt/ Sdcard2. There is no sdcard2 this directory in the supplied simulator, because the emulator does not partition the ROM out of the internal storage.
Through the code, the acquisition of internal space, the discovery of different ways to get the phone is different: for example, my Huawei mobile phone, access to the internal storage of the phone, the actual access to the/data partition space, and in some of the phone is the ROM partition after the remaining space (and we call the internal storage space)
There are two situations when acquiring sdcard space:
1, when the phone is not plugged into the SD card, the total SD card space obtained is the remaining internal storage space after the ROM is divided.
2, when the phone plug in the SD card, then get the total SD card space for the external SD card space
3. SD card reading and writing
ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;ImportJava.io.InputStream;Importorg.apache.http.util.EncodingUtils;Importandroid.app.Activity; Public classFileAccess {/*** One, the file access under the Private folder (/data/data/package name/files) * *@paramFileName *@parammessage*/ Public voidwritefiledata (String fileName, String message) {Try{fileoutputstream Fout=openfileoutput (FileName, mode_private); byte[] bytes =message.getbytes (); Fout.write (bytes); Fout.close (); } Catch(Exception e) {e.printstacktrace (); } } /***//Read the file under the./data/data/Package Name/files/* *@paramFileName *@return */ Publicstring Readfiledata (String fileName) {string res= ""; Try{fileinputstream fin=Openfileinput (fileName); intLength =fin.available (); byte[] buffer =New byte[length]; Fin.read (buffer); Res= encodingutils.getstring (buffer, "UTF-8"); Fin.close (); } Catch(Exception e) {e.printstacktrace (); } returnRes; } /*** Write, read sdcard directory files, to use FileOutputStream, can not use Openfileoutput * different points: Openfileoutput is compiled in raw, FileOutputStream is any file All can be *@paramFileName *@parammessage*/ //files written under the/mnt/sdcard/directory Public voidWritefilesdcard (String fileName, String message) {Try { //FileOutputStream fout = Openfileoutput (FileName, mode_private);FileOutputStream Fout =NewFileOutputStream (fileName); byte[] bytes =message.getbytes (); Fout.write (bytes); Fout.close (); } Catch(Exception e) {e.printstacktrace (); } } //read the file below the/mnt/sdcard/directory Publicstring Readfilesdcard (String fileName) {string res= ""; Try{fileinputstream fin=NewFileInputStream (fileName); intLength =fin.available (); byte[] buffer =New byte[length]; Fin.read (buffer); Res= encodingutils.getstring (buffer, "UTF-8"); Fin.close (); } Catch(Exception e) {e.printstacktrace (); } returnRes; } /*** Second, get the file from the Raw folder in resource and read the data (resource file can only read and write) * *@paramFileinraw *@return */ PublicString Readfromraw (intFileinraw) {String res= ""; Try{InputStream in=getresources (). Openrawresource (Fileinraw); intLength =in.available (); byte[] buffer =New byte[length]; In.read (buffer); Res= encodingutils.getstring (buffer, "GBK"); //res = new String (buffer, "GBK");In.close (); } Catch(Exception e) {e.printstacktrace (); } returnRes; } /*** Third, get the file from asset and read the data (resource file can only read and write) * *@paramFileName *@return */ Publicstring Readfromasset (String fileName) {string res= ""; Try{InputStream in=getresources (). Getassets (). open (FileName); intLength =in.available (); byte[] buffer =New byte[length]; In.read (buffer); Res= encodingutils.getstring (buffer, "UTF-8"); } Catch(Exception e) {e.printstacktrace (); } returnRes; } }
Android ROM partition with SD card read/write