Android File Listener

Source: Internet
Author: User

(1) Procedure description

1) First to add file creation, deletion, and write permission to the data

2) then expand Fileobseerver, write the SDK file listener class. Can view the following file listener source code

3) How to start file monitoring?

For activity, startwatching () is typically called in the Onresume () method to initiate file monitoring.

Call Stopwatching () in the OnPause () method to cancel file monitoring.

(2) Layout file

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:orientation=" vertical "&G    T <relativelayout android:layout_width= "fill_parent" android:layout_height= "Wrap_content" Android:or ientation= "vertical" > <textview android:id= "@+id/label_01" android:layout_width= "Wrap_ Content "android:layout_height=" wrap_content "android:text=" @string/name "Android:textsiz            E= "20DP"/> <edittext android:id= "@+id/filename" android:layout_width= "160DP" android:layout_height= "Wrap_content" android:layout_aligntop= "@id/label_01" android:layout_toright of= "@id/label_01"/> </RelativeLayout> <textview android:id= "@+id/label_02" android:layout        _width= "Wrap_content"android:layout_height= "Wrap_content" android:text= "@string/neirong"/> <edittext android:id= "@+id/c        Ontent "android:layout_width=" fill_parent "android:layout_height=" 120px "android:ems=" > <requestfocus/> </EditText> <linearlayout android:layout_width= "Fill_parent" Android: layout_height= "wrap_content" android:orientation= "horizontal" > <button android:id= "@+id/sa Vebutton "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Androi            D:gravity= "Right" android:text= "@string/save"/> <button android:id= "@+id/readbutton" Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:gravity= " Right "android:text=" @string/read "/> </LinearLayout> <textview android:id=" @+id/textc Ontent "Android:lAyout_width= "Match_parent" android:layout_height= "Wrap_content" android:layout_weight= "0.10"/></Linea Rlayout>

(3) File Listener source code:

1) Extended Fileobserver for file or folder monitoring

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); BR Eak;case fileobserver.all_events:log.d ("All", "filename" +path);    

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; pri Vate TextView textcontent;  private static final String TAG = "Simplefile"; Sdcardlistener Myfilelistener = new Sdcardlistener ("/sdcard");p ublic void 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.setonclicklistener (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"; t ry {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 ();} Toast.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 not Present", 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;}}; }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.