从FileObserver问题到Android主要活动的Android处理程序消息

时间:2013-08-06 14:01:58

标签: android handlers fileobserver

我是Android新手,这是我的第一个项目,并且已经对处理程序 FileObservers 产生了一些困难。应用程序将观察文件系统上的文件夹,并且当文件被修改时,文件夹内的新图像将显示在UI上。

目前代码没有中断,但我一直在摸不着为什么我在 FileObserver 类中调用的 Handler 只触发一次。

如果我删除处理程序调用并修改监视文件夹中的多个文件, FileObserver 将获取所有更改并记录相关消息。

但就像之前提到的那样,当我允许处理程序触发 FileObserver 时,它只会获取对监视文件夹的第一次更改,而不是随后的任何内容。

主要活动

private static ImageView imgMAX;
private static ImageView imgMIN;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    imgMAX = (ImageView)findViewById(R.id.imageViewMAX);
    imgMIN = (ImageView)findViewById(R.id.imageViewMIN);
    vidAD = (VideoView)findViewById(R.id.videoViewAD);

    //Sorting out media player playback options
            MediaPlayer.OnPreparedListener PreparedListener = new MediaPlayer.OnPreparedListener(){

                 @Override
                 public void onPrepared(MediaPlayer m) {
                     try {
                            if (m.isPlaying()) {
                                m.stop();
                                m.release();
                                m = new MediaPlayer();
                            }
                            m.setVolume(0f, 0f);
                            m.setLooping(true);
                            m.start();
                        } catch (Exception e) {
                            e.printStackTrace();
                        } 
                 }
             };

    //Set up media controller for videos
    MediaController mediaController = new MediaController(this);
    mediaController.setAnchorView(vidAD);
    vidAD.setMediaController(new MediaController(this));
    vidAD.setOnPreparedListener(PreparedListener);

    //Create Message Handler
     handler = new Handler(Looper.getMainLooper()) 
     { 
        @Override
        public void handleMessage(Message msg) { 
              String type = msg.getData().getString("typeoperation"); 
              String path = msg.getData().getString("path"); 

              Log.d("MESSAGE HANDLER", "Message Recieved: "+ type + " " + path); 

              String splitchar = "/";
              String[] parts = path.split(splitchar);
              String file = parts[parts.length-1];

                  if (file.equals("min.jpg")){
                      Bitmap bmp = BitmapFactory.decodeFile(path);
                      imgMIN.setImageBitmap(bmp);

                      PathFileObserver pfo = new     PathFileObserver(filepath,handler); 
                      pfo.startWatching();  
                  } else if (file.equals("max.jpg")){
                      Bitmap bmp = BitmapFactory.decodeFile(path);
                      imgMAX.setImageBitmap(bmp);

                      PathFileObserver pfo = new PathFileObserver(filepath,handler); 
                      pfo.startWatching();  
                  } else if (file.equals("video.mp4")){
                      vidAD.stopPlayback();
                      vidAD.setVideoPath(path);
                      vidAD.requestFocus();
                      vidAD.start();
                  }
              } 
     }; 

    //Check if Storage is available
    if(isExternalStorageReadable()){
        filepath = Environment.getExternalStorageDirectory()+File.separator;
        File f = new File(Environment.getExternalStorageDirectory());
        if(f.exists()){
            Bitmap bmp = BitmapFactory.decodeFile(filepath+"max.jpg");
            imgMAX.setImageBitmap(bmp);

            bmp = BitmapFactory.decodeFile(filepath+"min.jpg");
            imgMIN.setImageBitmap(bmp);

            vidAD.setVideoPath(filepath+"video.mp4");
            vidAD.requestFocus();
            vidAD.start();

            PathFileObserver pfo = new PathFileObserver(filepath,handler); 
            pfo.startWatching(); 
        }else{
            new AlertDialog.Builder(this).setTitle("Error").setMessage("Error Loading Content.").setNeutralButton("Close", null).show();
        }   
    }
}

FileObserver

public class PathFileObserver extends FileObserver{
static final String TAG="FILEOBSERVER";
String rootPath;

static final int mask = (FileObserver.CREATE | 
        FileObserver.DELETE | 
        FileObserver.DELETE_SELF |
        FileObserver.MODIFY |
        FileObserver.MOVED_FROM |
        FileObserver.MOVED_TO |
        FileObserver.MOVE_SELF |
        FileObserver.CLOSE_WRITE);
private Handler handler; 

public PathFileObserver(String root,Handler handler){
    super(root, mask);

    if (! root.endsWith(File.separator)){
        root += File.separator;
    }
    rootPath = root;
    this.handler = handler;
}

public void onEvent(int event, String path) {

    Message msg = Message.obtain(handler); 
    Bundle b = new Bundle(); 

    switch(event){
    case FileObserver.CREATE:
        Log.d(TAG, "CREATE:" + rootPath + path);
        break;
    case FileObserver.DELETE:
        Log.d(TAG, "DELETE:" + rootPath + path);
        break;
    case FileObserver.DELETE_SELF:
        Log.d(TAG, "DELETE_SELF:" + rootPath + path);
        break;
    case FileObserver.MODIFY:
        Log.d(TAG, "MODIFY:" + rootPath + path);
        break;
    case FileObserver.MOVED_FROM:
        Log.d(TAG, "MOVED_FROM:" + rootPath + path);
        break;
    case FileObserver.MOVED_TO:
        Log.d(TAG, "MOVED_TO:" + path);
        break;
    case FileObserver.MOVE_SELF:
        Log.d(TAG, "MOVE_SELF:" + path);
        break;
    case FileObserver.CLOSE_WRITE:
        Log.d(TAG, "CLOSE_WRITE:" + path);

            b.putString("typeoperation","CLOSE_WRITE"); 
            b.putString("path",rootPath + path); 
            msg.setData(b); 
            handler.sendMessage(msg); 

        break;
    default:
        // just ignore
        break;
    }

}

public void close(){
    super.finalize();
}
}

修改

因此,在进行了一些开发和处理视频视图后,我发现通过将 FileObserver 中的onevent更改为:

case FileObserver.CLOSE_WRITE:
        Log.d(TAG, "CLOSE_WRITE:" + path);

            b.putString("typeoperation","CLOSE_WRITE"); 
            b.putString("path",rootPath + path); 
            msg.setData(b); 
            handler.sendMessage(msg); 

        break;

在我的处理程序上填写 VideoView 的代码:

else if (file.equals("video.mp4")){
  Toast.makeText(getBaseContext(), "Buffering New Video...", Toast.LENGTH_LONG).show();
      vidAD.stopPlayback();
          vidAD.setVideoPath(path);
          vidAD.requestFocus();
          vidAD.start();
}

当新视频加载到文件夹并被 FileObserver 选中时,它会按预期加载视频,然后如果我尝试在视频后立即加载图像 FileObserver 立即获取新图像,而无需创建文件观察器实例。

0 个答案:

没有答案