Android使用FileObserver检测摄像头捕获事件

时间:2016-06-19 15:24:48

标签: android android-camera fileobserver

如何检测用户何时在相机中拍照?我正在服役。我想得到它的字节数据。

String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Camera";
fileObserver = new FileObserver(path) {
    @Override
    public void onEvent(int event, String file) {

        Log("File: " + file);
    }
};

fileObserver.startWatching();

1 个答案:

答案 0 :(得分:1)

您必须观看FileObserver的CREATE事件。

String PATH = Environment.getExternalStorageDirectory().getAbsolutePATH() + "/DCIM/Camera";
observer = new FileObserver(PATH) {
    @Override
    public void onEvent(int event, String file) {

        //if it's not CREATE event, return
        if(event != FileObserver.CREATE)
            return;

        byte[] bytes = new byte[0];
        String filePath = PATH + "/" + file;

        try {
            bytes = org.apache.commons.io.FileUtils.readFileToByteArray(new File(filePath));
        } catch (IOException e) {
            e.printStackTrace();
        }

        if(bytes.length == 0)
            return;

        //use byte data here
    }
};