Android provides file storage for removable external storage. Call Environment. Getexternalstoragestate () to check the available status of the sdcard when calling the external sdcard. The path of the SDcard is obtained by Environment.getexternalstoragedirectory () . Files written to external storage need to add authorization to SDcard
<!--write sdcard need to add write SDcard authorization--
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
BooleanMexternalstorageavailable= False;
BooleanMexternalstoragewriteable= False;
StringState= Environment.Getexternalstoragestate();
If (Environment.media_mounted.equals(State)) {
We can read and write the media
Mexternalstorageavailable=Mexternalstoragewriteable= True;
} Else If (Environment.Media_mounted_read_only.equals(State)) {
We can only read the media
Mexternalstorageavailable= True
mexternalstoragewriteable =false;
} else {
//Something else is wrong. It may being one of many other states, but all we need
// to kno W is we can neither read nor Write
mexternalstorageavailable = mexternalstoragewriteable = false
}
An example of the Android learning process is as follows:
file reading and Writing tool class:
1 PackageCom.example.android_data_storage_sdcard.file;2 3 ImportJava.io.File;4 Importjava.io.FileNotFoundException;5 ImportJava.io.FileOutputStream;6 Importjava.io.IOException;7 8 Importandroid.os.Environment;9 ImportAndroid.util.Log;Ten /** One * @authorXiaowu A * External Storage of @note file storage (SDcard storage). - * External storage does not require a context - */ the Public classFileuitls { - Private FinalString TAG = "Fileutil"; - - /** + * Save the file to an external storage SD card - * + * @paramFileName A * @paramMode at * @paramData - * @return - */ - Public BooleanSavefiletosdcard (String FileName,byte[] data) { - BooleanFlag =false; - //judging the state of SDcard inString State =environment.getexternalstoragestate (); - //get sdcard root directory/mnt/sdcard/ ... toFile root =environment.getexternalstoragedirectory (); +FileOutputStream OutputStream =NULL; - //environment.media_mounted means the SD card is mounted on the phone and can read and write the if(Environment.MEDIA_MOUNTED.equals (state)) { * //create a file in the root directory of SDcard $File File =NewFile (root, fileName);Panax Notoginseng Try { -OutputStream =Newfileoutputstream (file); theOutputstream.write (data, 0, data.length); +Flag =true ; A}Catch(FileNotFoundException e) { theLOG.I (TAG, "The file did not find an exception!) "); +}Catch(Exception e) { -LOG.I (TAG, "Write file Exception"); $}finally { $ if(OutputStream! =NULL) { - Try { - outputstream.close (); the}Catch(IOException e) { -LOG.I (TAG, "Shutdown IO stream has an exception!"));Wuyi } the } - } Wu } - returnFlag; About } $}
JUnit Unit Test class:
The Unit test class must inherit from Androidtestcase and add the unit test package <uses-library android:name= "Android.test.runner"/> in the resource manifest file, and add the unit test target package
<!--introducing Unit test target packages--
<instrumentation
Android:name= "Android.test.InstrumentationTestRunner"
Android:targetpackage= "Com.example.android_data_storage_sdcard" >
</instrumentation>
1 PackageCom.example.android_data_storage_sdcard;2 3 Importandroid.test.AndroidTestCase;4 5 ImportCom.example.android_data_storage_sdcard.file.FileUitls;6 7 Public classMyTestextendsAndroidtestcase {8 9 Public voidTest () {TenFileuitls Fileuitls =NewFileuitls (); OneFileuitls.savefiletosdcard ("Keji.txt", "Orange Island Head". GetBytes ()); A } -}
/span>
Android Development-file storage read/write SDcard