سؤال

In my application I need to increase the count of photos when one is captured. So I set a FileObserver for the DCIM directory. It watches for captured photos.

However, this FileObserver stops watching when I unmount my SD card because the DCIM directory is no longer present. When I mount the SD card, I tried to register the FileObserver once again after I got android.intent.action.EXTERNAL_APPLICATIONS_AVAILABLE intent. But the FileObserver is not getting registered.

My questions are:

  1. Is there any other way to make the FileObserver watch after mounting the SD card?
  2. In some devices (HTC) I don't get any intents after I mount the SD card. What method can I use in that case?
  3. Is there any other method to listen to the Photo taken by user in his built in soft button camera. I don't want to built my own camera.

    fo = new FileObserver(path.toString(),
                        FileObserver.CLOSE_WRITE) {
    
                    @Override
                    public void onEvent(int event, String path) {
                        Log.d("yes", "event "+ event);
                        Log.d("operator",
                                "out side if"
                                        + Phototaken
                                        + externalStorageState
                                                .equals(Environment.MEDIA_MOUNTED));
                        if (Phototaken == 0 && event == 8){
                            String st = timeStamp();
                            Log.d("operator", "in event " + Phototaken);
                            Log.d("operator", "lat: " + MainService.lat
                                    + " " + "lng: " + MainService.lng + " "
                                    + "location: " + MainService.addre
                                    + " " + "time: " + st);
                            ptd.insert(st, String.valueOf(MainService.lat),
                                    String.valueOf(MainService.lng),
                                    MainService.addre);
                        }
    
                    }
                };
                fo.startWatching();
    
هل كانت مفيدة؟

المحلول

you can create a new BroadcastReceiver to watch for SD mount/unmount events. Then, on a mount event, tell your FileObserver to start watching again the DCIM directory.

You need to specify intent filters to watch for the events you want:

IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
filter.addAction(Intent.ACTION_MEDIA_REMOVED);

This snippet from the official API reference shows how to monitor the state of the external storage.

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