سؤال

My code listens to the DCIM folder, using a FileObserver.

All Android versions I used, except 4.1.1, sent only 1 event - when the video was finished taken. I think it's the correct behavior - write continually and close when finished.

In 4.1.1 (Galaxy Nexus and Nexus S) though, the event FileObserver.CLOSE_WRITE is sent twice - when the video starts and when it ends.

Also the same for photos - the event is sent twice - though it's not that critical.

The problem is that I can't distinguish between the start event and end event of a video.

I could try and check the size of the file, but because the event may have been delayed (slow/busy device), the size may be quite big.

Any idea why was the behavior changed? Do you know where is the camera's app source code? I can try and look at the history to understand that.

هل كانت مفيدة؟

المحلول

As I wrote in one of my comments, the difference between 4.1 and previous Android versions is that in 4.1.1, the file is written and closed twice. Once when an empty video file is created. Then the video is written into a tmp file. Then the rename/copy of the tmp file is the second write_close event.

In previous versions there's not tmp file - only the original - thus only one close_write event.

Please comment if you think it's a bug. I'm not sure.

نصائح أخرى

I have myself an app which monitors the DCIM/Camera directory through a FileObserver. What I noticed, and could be of help to you, is that the first operation is a CLOSE_WRITE, however the final operation is a MOVED_TO from the .tmp to the real file, which means you can recognize when the video is (really) ready.

My real code is more complex due to the requirements of my app, but the general idea is something like this:

/* My FileObserver implementation field */
private HashSet<String> jbCache = new HashSet(...)

...

protected void onEvent(int event, String path) {
   boolean isJellyBean = Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLYBEAN;

   if ((event & FileObserver.CLOSE_WRITE) > 0) {
      if (isJellyBean) {
         jbCache.add(path);
      } else {
         performYourWork(path);
      }
   } else if ((event & FileObserver.MOVED_TO) > 0 && isJellyBean && jbCache.contains(path)) {
      performYourWork(path);
      jbCache.remove(path);
   }
}

You have to listen to both CLOSE_WRITE and MOVED_TO when you register the events you want to catch, obviously.

Although I starred your bug, I doubt Google will ever acknowledge it, as it looks like there could be some (disagreeable) reasoning behind the change. The Camera app is mostly a no-standard crap anyway (e.g.: fake DCIM standard compliance)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top