(1) Procedures
1) first, you must add the permissions for file creation, deletion, and Data Writing.
2) Extend fileobseerver and write the SDk file listening class. You can view the following file listener source code
3) How to start file monitoring?
For an Activity, startwatching () is usually called in the onResume () method to start file monitoring.
Call stopwatching () in the onPause () method to cancel file monitoring.
(2) Layout File
(3) file listener source code:
1) added the Fileobserver extension to monitor files or folders.
package com.liuzuyi.fileobserver;import android.os.FileObserver;import android.util.Log;public class SDCardListener extends FileObserver {public SDCardListener(String path) { super(path);}public void onEvent(int event, String path) {switch (event) {case FileObserver.CREATE :Log.d("creat", "filename"+path);break;case FileObserver.ALL_EVENTS :Log.d("all", "filename"+path);break;}}}
2) Main program code
Package com. liuzuyi. fileobserver; import java. io. byteArrayOutputStream; import java. io. file; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. inputStream; import java. io. outputStream; import android. app. activity; import android. OS. bundle; import android. OS. environment; import android. util. log; import android. view. menu; import android. view. menuItem; import android. view. view; import Android. view. view. onClickListener; import android. widget. button; import android. widget. editText; import android. widget. textView; import android. widget. toast; public class MainActivity extends Activity {private EditText filename; private EditText content; private TextView textcontent; private static final String TAG = "simplefile "; SDCardListener myfileListener = new SDCardListener ("/sdcard"); public voi D onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); filename = (EditText) findViewById (R. id. filename); content = (EditText) findViewById (R. id. content); textcontent = (TextView) findViewById (R. id. textcontent); Button savebutton = (Button) this. findViewById (R. id. savebutton); Button viewbutton = (Button) this. findViewById (R. id. readbutton); savebutton. setO NClickListener (listener); viewbutton. setOnClickListener (listener);} public void onResume () {super. onResume (); myfileListener. startWatching ();} public void onPause () {super. onPause (); myfileListener. stopWatching ();} private View. onClickListener listener = new OnClickListener () {public void onClick (View v) {Button button = (Button) v; String namestr = filename. getText (). toString (). trim (); String contentStr = Content. getText (). toString (); switch (button. getId () {case R. id. savebutton: String res = "success"; try {if (Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED) {FileOutputStream outstream = new FileOutputStream ("/sdcard/" + namestr + ". txt "); outstream. write (contentStr. getBytes (); outstream. close ();} else return;} catch (Exception e) {res = "failure"; e. printStackTrace ();} T Oast. makeText (MainActivity. this, res, Toast. LENGTH_LONG ). show (); Log. I (TAG, namestr); Log. I (TAG, contentStr); break; case R. id. readbutton: String resid_v = "success"; String contentst = null; try {if (Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED) {File SDCardDir = Environment. getExternalStorageDirectory (); File saveFile = new File (SDCardDir, namestr + ". txt "); FileInputStream instream = New FileInputStream (saveFile); ByteArrayOutputStream Ostream = new ByteArrayOutputStream (); byte [] buffer = new byte [1024]; int len =-1; while (len = instream. read (buffer ))! =-1) {Ostream. write (buffer, 0, len);} contentst = Ostream. toString (); Ostream. close (); instream. close ();} else {Toast. makeText (MainActivity. this, "SD card does not exist", Toast. LENGTH_LONG ). show () ;}} catch (Exception e) {resid_v = "faile"; e. printStackTrace ();} textcontent. setText (contentst); Log. I (TAG, contentst); Toast. makeText (MainActivity. this, resid_v, Toast. LENGTH_LONG ). show (); Log. I (TAG, namestr); break ;}}};}