1

我需要使用相机意图并跟踪该意图拍摄的新图像。由于我需要一次拍摄多张照片,因此我使用了 intent MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA。这是我的代码:

package com.arko.cameraintent;

import android.content.Intent;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import java.io.File;

public class CameraMainActivity extends AppCompatActivity {
    private static final int REQ_CODE = 0;
    AppCompatActivity main_activity = this;  // save it for future use
    DirectoryFileObserver directoryFileObserver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera_main);

    }

    public void takePhoto(View view)  // fired when a button in the main application is clicked. This starts the camera application.
    {

        Intent callCamAppIntent = new Intent();
        callCamAppIntent.setAction(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
        String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath();
        Toast.makeText(this, dir, Toast.LENGTH_SHORT).show();
        directoryFileObserver = new DirectoryFileObserver(dir);
        directoryFileObserver.startWatching();
        startActivityForResult(callCamAppIntent, REQ_CODE);


    }


    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == REQ_CODE){
            Toast.makeText(this, "picture taken successfully", Toast.LENGTH_SHORT).show();
            directoryFileObserver.stopWatching();
        }
    }

}

类代码DirectoryFileObserver

package com.arko.cameraintent;

import android.os.AsyncTask;
import android.os.Environment;
import android.os.FileObserver;
import android.support.annotation.Nullable;

import java.io.File;


public class DirectoryFileObserver extends FileObserver {
    String rootDir;
    static final int mask = (FileObserver.CREATE | FileObserver.MODIFY | FileObserver.DELETE);
    public final String DIRECTORY_NAME = "Simplified Intent";

    public DirectoryFileObserver(String path) {

        super(path, mask);
        rootDir = path;
    }

    @Override
    public void onEvent(int event, @Nullable String path) {
        switch(event){
            case FileObserver.CREATE:
                String outputDir = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator +
                        Environment.DIRECTORY_PICTURES +
                        File.separator + DIRECTORY_NAME;

                // do something here
                break;

            case FileObserver.MODIFY:
                // will be implemented later
                break;
            case FileObserver.DELETE:
                // will be implemented later
                break;
        }
    }


}

onEvent正如 Debug 所建议的那样,该函数永远不会被执行。背后的原因是什么?我什至将相机应用程序的默认保存位置更改为内部存储(最初设置为 SD 卡)并再次运行我的应用程序,但没有任何改变。我已经在清单文件中 提到了READ_EXTERNAL_STORAGE和权限。WRITE_EXTERNAL_STORAGE

4

0 回答 0