標籤:android style blog class code java
(1)程式說明
1)首先要添加檔案建立,刪除,和寫入資料的許可權
2)接著擴充fileobseerver,寫SDk檔案監聽類。可以查看下文的檔案監聽器源碼
3)如何開機檔案監控?
對於Activity來說通常在onResume()方法中調用startwatching()來開機檔案監控。
在onPause()方法中調用stopwatching()來取消檔案監控。
(2)布局檔案
<?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" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/label_01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/name" android:textSize="20dp" /> <EditText android:id="@+id/filename" android:layout_width="160dp" android:layout_height="wrap_content" android:layout_alignTop="@id/label_01" android:layout_toRightOf="@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/content" android:layout_width="fill_parent" android:layout_height="120px" android:ems="10" > <requestFocus /> </EditText> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/savebutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android: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/textcontent" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.10" /></LinearLayout>
(3)檔案監聽器源碼:
1)擴充的Fileobserver,實現檔案或者檔案夾的監聽
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)主程式碼
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 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"; 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();} 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卡不存在", 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;}}}; }