Android 相机:数据意图返回 null [英] Android Camera : data intent returns null

查看:35
本文介绍了Android 相机:数据意图返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个活动的 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();
        }
    }
}

问题是意图data为空,OnActivityResult方法直接转为(resultCode == RESULT_CANCELED)和应用返回上一个活动.

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?

谢谢

推荐答案

默认的 Android 相机应用程序仅在返回的 Intent 中传回缩略图时才返回非 null Intent.如果您传递带有要写入的 URI 的 EXTRA_OUTPUT,它将返回一个 null 意图,并且图片位于您传入的 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.

您可以通过在 GitHub 上查看相机应用程序的源代码来验证这一点:

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

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.

这篇关于Android 相机:数据意图返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆