我最近在网上搜了一段关于android监听文件夹的程序可是不知道哪里有错,请大神帮助看看。public class SDCardListener extends FileObserver {
private Context context; public SDCardListener(Context context, String path) {
// 这种构造方法是默认监听所有事件的,如果使用super(String,int)这种构造方法, 则int参数是要监听的事件类型.
super(path);
this.context = context;
init();
}

/**
 * 注册监听器
 */
public void init() {
IntentFilter intentfilter = new IntentFilter(Intent.ACTION_MEDIA_SCANNER_STARTED);
intentfilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
intentfilter.addDataScheme("file");
ScanSdReceiver scanSdReceiver = new ScanSdReceiver();
context.registerReceiver(scanSdReceiver, intentfilter);
} @Override
public void onEvent(int event, String path) {
switch (event) {
case FileObserver.CREATE://监听创建
Log.d("Create", "path:" + path);
rescanSdcard();
break;
case FileObserver.DELETE://监听删除
Log.d("Delete", "path:" + path);
rescanSdcard();
break;
}
}

/**
 * 刷新SD卡
 */
public void rescanSdcard() {
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, 
Uri.parse("file://"+ Environment.getExternalStorageDirectory().getAbsolutePath())));
}
}