In order to better access the application's large file data, the application needs to be read. Write the file on the SD card. SD card greatly expands the storage capacity of the mobile phone.
Operation SD First to add permissions:
<!--Create and delete file permissions in SDcard --><uses-permission android:name= "android.permission.MOUNT_UNMOUNT_ Filesystems "/><!--Write Data permissions to SDcard--><uses-permission android:name=" android.permission.WRITE_EXTERNAL_ STORAGE "/><!-- Read data permissions from SDcard--><uses-permission android:name=" android.permission.READ_EXTERNAL_ STORAGE "/>
To read and write the files on the SD card, please follow the following three steps:
(1) Call Environment's Getexternalstoragestate () method to determine if the phone is plugged into an SD card, and the application has access to read and write the SD card;
(2) Call Environment's getExternalStorageDirectory () method to obtain the external memory, that is, the SD card directory;
(3) Use FileInputStream FileOutputStream FileReader or FileWriter to read and write the files inside the SD card;
The following is a simple example description:
1.XML Code:
1 <Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 Android:paddingbottom= "@dimen/activity_vertical_margin"6 Android:paddingleft= "@dimen/activity_horizontal_margin"7 Android:paddingright= "@dimen/activity_horizontal_margin"8 Android:paddingtop= "@dimen/activity_vertical_margin"9 Tools:context=". Mainactivity " >Ten One <Button A Android:id= "@+id/button1" - Android:layout_width= "Match_parent" - Android:layout_height= "Wrap_content" the Android:layout_alignparenttop= "true" - Android:layout_centerhorizontal= "true" - Android:text= "Write data to SD" /> - + <Button - Android:id= "@+id/button2" + Android:layout_width= "Match_parent" A Android:layout_height= "Wrap_content" at Android:layout_alignleft= "@+id/button1" - Android:layout_below= "@+id/button1" - Android:text= "read data from SD card" /> - - </Relativelayout>
2.java Code:
1 Packagecom.example.filesd;2 3 ImportJava.io.BufferedReader;4 ImportJava.io.File;5 ImportJava.io.FileInputStream;6 ImportJava.io.FileOutputStream;7 ImportJava.io.InputStreamReader;8 ImportAndroid.os.Bundle;9 Importandroid.os.Environment;Ten Importandroid.app.Activity; One ImportAndroid.view.View; A ImportAndroid.view.View.OnClickListener; - ImportAndroid.widget.Button; - ImportAndroid.widget.Toast; the - Public classMainactivityextendsActivity { - Privatebutton button; - PrivateButton button2; + @Override - protected voidonCreate (Bundle savedinstancestate) { + Super. OnCreate (savedinstancestate); A Setcontentview (r.layout.activity_main); atButton = (Button) This. Findviewbyid (r.id.button1); -Button2 = (Button) This. Findviewbyid (r.id.button2); -Button.setonclicklistener (NewOnclicklistener () { - @Override - Public voidOnClick (View arg0) { -Writesdcard ("Hello, congratulations on your data read!") "); in } - }); toButton2.setonclicklistener (NewOnclicklistener () { + @Override - Public voidOnClick (View arg0) { the Readsdcard (); * $ }Panax Notoginseng }); - } the + //writing data to an SD card A Private voidWritesdcard (String str) { the Try { + //determine if an SD card exists - if(Environment.getexternalstoragestate (). Equals ( $ environment.media_mounted)) { $ //get the SD card directory -File Sddire =environment.getexternalstoragedirectory (); -FileOutputStream Outfilestream =NewFileOutputStream ( theSddire.getcanonicalpath () + "/test.txt"); - Outfilestream.write (Str.getbytes ());Wuyi outfilestream.close (); theToast.maketext ( This, "Data is saved to Text.txt file.", Toast.length_long) - . Show (); Wu } -}Catch(Exception e) { About e.printstacktrace (); $ } - } - - A //reading data from the SD card + Private voidReadsdcard () { theStringBuffer Strsbuffer =NewStringBuffer (); - Try { $ //determine if there is an SD the if(Environment.getexternalstoragestate (). Equals ( the environment.media_mounted)) { theFile File =NewFile (environment.getexternalstoragedirectory () the. Getcanonicalpath () + "/test.txt"); - //determine if the file exists in if(File.exists ()) { the //Open File input stream theFileInputStream FileR =Newfileinputstream (file); AboutBufferedReader reads =NewBufferedReader ( the NewInputStreamReader (FileR)); theString st =NULL; the while((st = Reads.readline ())! =NULL) { + Strsbuffer.append (ST); - } the filer.close ();Bayi}Else { theToast.maketext ( This, "The file does not exist in this directory", Toast.length_long). Show (); the } - } -}Catch(Exception e) { the e.printstacktrace (); the } theToast.maketext ( This, "The data read is:" + strsbuffer.tostring () + "", the Toast.length_long). Show (); - } the}
SD card for Android data storage