Android's operation of SDcard expansion card files is a normal file operation, but there are still places to pay attention to. Like what:
1. Join SDcard Operation Authority ;
2. recognizes the existence of sdcard ;
3. You cannot create files directly in a sdcard root directory, but you need to create a directory first and then create a file ;
Examples are as follows:
(1) Add SDcard operation permission in Androidmanifest.xml
<!--sdcard permissions-->
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE" ></ Uses-permission>
(2) Variable declaration:
Private final static String PATH = "/sdcard/digu";
Private final static String FILENAME = "/notes.txt";
(3) write files to SDcard:
/**
* Write file
/
private void Onwrite () {
try {
log.d (log_tag, "Start write");
1. To determine whether there is a sdcard
if (Environment.MEDIA_MOUNTED.equals (Environment
. Getexternalstoragestate ())) {
// Directory
File Path = new file (path);
File "
f = new" (PATH + FILENAME);
if (!path.exists ()) {
//2. Create a directory, you can create path.mkdirs () when the application is started
();
if (!f.exists ()) {
//3. create file
f.createnewfile ();
}
OutputStreamWriter OSW = new OutputStreamWriter (
new FileOutputStream (f));
4. Write the file and get the text value from EditView
osw.write (Editor.gettext (). toString ());
Osw.close ();
}
catch (Exception e) {
log.d (log_tag, "file Create Error");
}