Android摄像头:数据意图返回null意图、摄像头、数据、Android

2023-09-12 00:12:13 作者:眉梢带笑

我有一个Android应用程序,其中包含多个活动。

I have an android application which contains multiple activities.

在其中的一个我用一个按钮,将调用设备摄像头:

In one of them I'm using a button which will call the device camera :

public void onClick(View view) {
    Intent photoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(photoIntent, IMAGE_CAPTURE);
}

在相同的活动我称之为 OnActivityResult 法的图像结果是:

In the same activity I call the OnActivityResult method for the image result :

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == IMAGE_CAPTURE) {
        if (resultCode == RESULT_OK) {
            Bitmap image = (Bitmap) data.getExtras().get("data");
            ImageView imageview = (ImageView) findViewById(R.id.pic);
            imageview.setImageBitmap(image);}
        else if (resultCode == RESULT_CANCELED) 
            {Toast.makeText(this, "CANCELED ", Toast.LENGTH_LONG).show();}
    }
}

问题是,意图数据为空和 OnActivityResult 方法转向直接将(结果code == RESULT_CANCELED)和应用程序返回到previous avtivity。

The problem is that the intent data is null and the OnActivityResult method turns directly to the (resultCode == RESULT_CANCELED) and the application returns to the previous avtivity.

我怎样才能解决这个问题,并调用摄像头,应用程序返回到其中包含当前活动后的的ImageView 这将包含拍摄的照片?

How can I fix this issue and after calling the camera, the application returns to the current activity which contains an ImageView which will contains the picture taken?

感谢

推荐答案

默认的Andr​​oid摄像头应用程序只传回来时,缩略图在返回的意图返回一个非空的意图。如果你通过 EXTRA_OUTPUT 与URI来写,它会返回一个意图和图片是在URI您在过去了。

The default Android camera application returns a non-null intent only when passing back a thumbnail in the returned Intent. If you pass EXTRA_OUTPUT with a URI to write to, it will return a null intent and the picture is in the URI that you passed in.

您可以在相机应用程序的源$ C ​​$ C在GitHub上验证这一点:

You can verify this by looking at the camera app's source code on GitHub:

https://github.com/android/platform_packages_apps_camera/blob/gingerbread-release/src/com/android/camera/Camera.java#L1186

Bundle newExtras = new Bundle();
if (mCropValue.equals("circle")) {
    newExtras.putString("circleCrop", "true");
}
if (mSaveUri != null) {
    newExtras.putParcelable(MediaStore.EXTRA_OUTPUT, mSaveUri);
} else {
    newExtras.putBoolean("return-data", true);
}

我猜想,你要么传递 EXTRA_OUTPUT 不知何故,或在您的手机的工作原理不同的摄像头应用程序。

I would guess that you're either passing in EXTRA_OUTPUT somehow, or the camera app on your phone works differently.