拍摄照片或录像时调用动作的最佳方式?

时间:2015-02-16 13:10:08

标签: android android-intent camera

我想在拍摄照片或视频时触发Asynctask,但是当我使用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" />

所以我的问题是:每当文件被添加到指定文件夹时触发我的Asynctask会更可靠吗? (例如DCIM /相机文件夹)?如果是这样,怎么办呢?

1 个答案:

答案 0 :(得分:1)

我能够用这个

做到这一点
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();

但如果由于其他原因图片未保存在DCIM文件夹中,您可以在所有文件夹上设置文件服务器

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