Read/write SD card
The Openfileinput and Openfileoutput methods of the context class are file operations for the application's Data folder, which has some limitations due to the limited ROM capacity of the phone.
The SD card of the phone provides a larger storage capacity than the ROM. More suitable for accessing large files.
environment[Android.os. Environment]
Constants and methods |
Type |
Describe |
public static final String media_mounted |
Constant |
Extended storage set allows read and write access |
public static final String media_checking |
Constant |
Extended storage set in check state |
public static final String media_mounted_read_only |
Constant |
Extended storage set in read-only state |
public static final String media_removed |
Constant |
Extended storage setting does not exist |
public static final String media_unmounted |
Constant |
The extended storage setting was not found |
public static File Getdatadirectory () |
Ordinary |
Get the Data Directory |
public static File Getdownloadcachedirectory () |
Ordinary |
Get the downloaded cache directory |
public static File getExternalStorageDirectory () |
Ordinary |
Get the Extended storage directory |
public static String Getexternalstoragestate () |
Ordinary |
Get the status of an extended storage device |
public static File Getrootdirectory () |
Ordinary |
Get root directory |
public static Boolean isexternalstorageremovable () |
Ordinary |
Determine if the extended storage directory is deleted |
Request read and write access to the SD card
Open the project manifest file and select Permissions in the Permission tab
SD card File read operation
1 PrivateTextView TV; 2 PrivateButton bt1;3 PrivateButton BT2; 4 PrivateEditText et;5 6 protected voidonCreate (Bundle savedinstancestate) {7 Super. OnCreate (savedinstancestate);8 Setcontentview (r.layout.xml file);9ET =(EditText) Findviewbyid (r.id.sdet);TenTV =(TextView) Findviewbyid (R.ID.SDTV); OneBT1 =(Button) Findviewbyid (r.id.sdbt01); ABT2 =(Button) Findviewbyid (r.id.sdbt02); -Bt1.setonclicklistener ( This); -Bt2.setonclicklistener ( This); the } - - Public voidOnClick (View v) { - if(!environment.getexternalstoragestate (). Equals ( +environment.media_mounted)) {//determine if the SD card exists -Toast.maketext (Sd0. This, "SD card does not exist", 3000). Show (); + return; A } at Switch(V.getid ()) { - Caser.id.sdbt01: - //Create a Folder object -File dir =NewFile (Environment.getexternalstoragedirectory (), "test"); - if(!dir.exists ()) {//If the directory does not exist, create - Dir.mkdir (); in } -File File =NewFile (dir, "test.txt");//Create a file to PrintWriter PW; + Try { -PW =Newprintwriter (file); the pw.println (Et.gettext (). toString ()); * pw.close (); $Toast.maketext (Sd0. This, "saved successfully", 3000). Show ();Panax Notoginseng}Catch(FileNotFoundException e) { - e.printstacktrace (); the } + Break; A Caser.id.sdbt02: theFile f =NewFile (environment.getexternalstoragedirectory () + +"/test/test.txt");//Create a Folder object - bufferedreader BR; $ Try { $BR =NewBufferedReader (NewInputStreamReader (NewFileInputStream (f))); -String str =NULL; - while(str = br.readline ())! =NULL) { the tv.append (str); - }Wuyi}Catch(FileNotFoundException e) { the e.printstacktrace (); -}Catch(IOException e) { Wu e.printstacktrace (); - } About Break; $ } -}
View Code
SD Card Operation "Android 8"