Android摄像头拍照功能不调用回调函数

Android摄像头拍照功能不调用回调函数,android,camera,android-camera,Android,Camera,Android Camera,我正在为我的应用程序进行自定义照相机活动。 我是按照安卓开发者网站的指示来做的: 除了不调用回调函数和不保存图片之外,一切似乎都正常。这是我的密码: public class CameraActivity extends Activity { private Camera mCamera; private CameraPreview mPreview; private static final String TAG = "CameraActivity"; /** Called when the

我正在为我的应用程序进行自定义照相机活动。 我是按照安卓开发者网站的指示来做的: 除了不调用回调函数和不保存图片之外,一切似乎都正常。这是我的密码:

public class CameraActivity extends Activity {
private Camera mCamera;
private CameraPreview mPreview;
private static final String TAG = "CameraActivity";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.camera);

    // Create an instance of Camera
    mCamera = getCameraInstance();

    // Create our Preview view and set it as the content of our activity.
    mPreview = new CameraPreview(this, mCamera);
    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
    preview.addView(mPreview);

    Button captureButton = (Button) findViewById(R.id.button_capture);
    captureButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.v(TAG, "will now take picture");
            mCamera.takePicture(null, null, mPicture);
            Log.v(TAG, "will now release camera");
            mCamera.release();
            Log.v(TAG, "will now call finish()");
            finish();
        }
    });        
}

private PictureCallback mPicture = new PictureCallback() {

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        Log.v(TAG, "Getting output media file");
        File pictureFile = getOutputMediaFile();
        if (pictureFile == null) {
            Log.v(TAG, "Error creating output file");
            return;
        }
        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();
        } catch (FileNotFoundException e) {
            Log.v(TAG, e.getMessage());
        } catch (IOException e) {
            Log.v(TAG, e.getMessage());
        }
    }
};

private static File getOutputMediaFile() {
    String state = Environment.getExternalStorageState();
    if (!state.equals(Environment.MEDIA_MOUNTED)) {
        return null;
    }
    else {
        File folder_gui = new File(Environment.getExternalStorageDirectory() + File.separator + "GUI");
        if (!folder_gui.exists()) {
            Log.v(TAG, "Creating folder: " + folder_gui.getAbsolutePath());
            folder_gui.mkdirs();
        }
        File outFile = new File(folder_gui, "temp.jpg");
        Log.v(TAG, "Returnng file: " + outFile.getAbsolutePath());
        return outFile;
    }
}
点击按钮后,我得到日志:“现在将拍照”,“现在将释放相机”和“现在将调用完成”。活动成功完成,但在过程中未调用回调函数

mCamera.takePicture(null, null, mPicture);
函数(没有来自mPicture回调或getMediaOutputFile函数的日志),并且指定的位置中没有文件

有什么想法吗?:)
非常感谢

mCamera.takePicture()的调用是异步的。这意味着呼叫会立即完成,但实际的拍照和处理会在稍后的某个时间进行。但是,从调用
mCamera.takePicture()
返回后,您会立即执行此操作:

这意味着您已经释放了相机资源,并在相机有机会实际拍照并给您回电话之前完成了活动


您需要将该代码移动到回调方法
onPictureTaken()
,以便在回调发生后释放资源并完成活动。

使用SurfaceView自定义相机单击一次即可拍摄多张图像,请按此操作

clickbtn.setOnClickListener(new OnClickListener() {

        @SuppressWarnings("deprecation")
        @Override
        public void onClick(View v) {

            for (int i = 0; i < 5; i++) {
                mcamera = mPreview.getCamera();

                getimage(mcamera);

                Toast.makeText(getApplicationContext(),
                        "multiple image count=" + i, 1000).show();

            }

            if (mPreview == null) {
                mPreview = new CamLayer(MainActivity.this);
            }


        }
    });

这很简单…我有点惊讶我在这里找不到类似的问题。不管怎样,我做到了,非常感谢:)我没有打电话给
mCamera.release()
finish()
,并且仍然有这个问题。所以可能发生了其他事情。@IgorGanapolsky也许你应该打开一个新问题,而不是在这个答案上添加注释。显然你的情况不同。原来的海报似乎对这个答案很满意。我需要一些C#的帮助。。请问为什么要将空值传递给
takePicture
method参数?
clickbtn.setOnClickListener(new OnClickListener() {

        @SuppressWarnings("deprecation")
        @Override
        public void onClick(View v) {

            for (int i = 0; i < 5; i++) {
                mcamera = mPreview.getCamera();

                getimage(mcamera);

                Toast.makeText(getApplicationContext(),
                        "multiple image count=" + i, 1000).show();

            }

            if (mPreview == null) {
                mPreview = new CamLayer(MainActivity.this);
            }


        }
    });
    public static void getimage(Camera mcamera) {


    PictureCallback mPicture = new PictureCallback() {

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            pictureFile = getOutputMediaFile();
            Log.e("path file 11111111", ""+pictureFile);
            if (pictureFile == null) {
                return;
            }
            try {
                FileOutputStream fos = new FileOutputStream(pictureFile);
                fos.write(data);
                fos.close();

            } catch (FileNotFoundException e) {

            } catch (Exception e) {

            }
        }
    };
    try {

        mcamera.startPreview();
        mcamera.takePicture(null, null, mPicture);
        Thread.sleep(2000);

    } catch (Exception exception) {
        exception.printStackTrace();
    }

}

private static File getOutputMediaFile() {
    File mediaStorageDir = new File(
            Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            "Sunil3");
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {

            return null;
        }
    }


    File mediaFile = new File(String.format(mediaStorageDir+File.separator+"%d.jpg", System.currentTimeMillis()));

    return mediaFile;
}