Ian M
Ian M

Reputation: 597

Best way to invoke an action when photo or video is taken?

I want to trigger an Asynctask whenever a photo or video is captured, but none of the intents below seem to be fired when I take a photo using the stock camera app on my Galaxy S4:

        <action android:name="android.intent.action.CAMERA_BUTTON" />
        <action android:name="com.android.camera.NEW_PICTURE" />
        <action android:name="android.hardware.action.NEW_PICTURE" />
        <category android:name="android.intent.category.DEFAULT" />

So my question is: would it be more reliable to trigger my Asynctask whenever a file is added to a designated folder? (e.g. the DCIM/camera folder)? If so, how can that be done?

Upvotes: 1

Views: 566

Answers (1)

danidee
danidee

Reputation: 9624

I was able to do it using this

FileObserver observer = new FileObserver(android.os.Environment.getExternalStorageDirectory().toString() + "/DCIM/100MEDIA") { // set up a file observer to watch the DCIM directory
            @Override
        public void onEvent(int event, String file) {
            if(event == FileObserver.CREATE && !file.equals(".probe")){ // check if its a "create" and not equal to .probe because thats created every time camera is started
                Log.d(TAG, "File created [" + android.os.Environment.getExternalStorageDirectory().toString() + "/DCIM/100MEDIA/" + file + "]");
                fileSaved = "New photo Saved: " + file;

//You could do other stuff here

            }
        }
    };
    observer.startWatching();

But if for some other reason the pictures are not getting saved in the DCIM folder you can set a fileobserver on all folders like this

FileObserver observer = new FileObserver(android.os.Environment.getExternalStorageDirectory().toString())

Upvotes: 1

Related Questions