The previous time I wrote common android operations on file access.
During Android development, you will also encounter access operations to files on sdcard.
The following operations write data to sdcard:
1 public void saveToSDCard(String filename, String content)throws Exception {2 File file = new File(Environment.getExternalStorageDirectory(), filename);3 FileOutputStream outStream = new FileOutputStream(file);4 outStream.write(content.getBytes());5 outStream.close();6 }
1 try {2 // determine whether the sdcard exists and can read and write 3 if (environment. getexternalstoragestate (). equals (environment. media_mounted) {4 savetosdcard (filename, content); 5 toast. maketext (getapplicationcontext (), R. string. success, 1 ). show (); 6} else {7 toast. maketext (getapplicationcontext (), R. string. sdcarderror, 1 ). show (); 8} 9} catch (exception e) {10 toast. maketext (getapplicationcontext (), R. string. fail, 1 ). show (); 11 E. printstacktrace (); 12}
First, you need to determine whether the sdcard can be written (consider writing protection) and whether it exists, and perform environment detection, and then write it into the memory card.