1

我正在尝试从相机捕获图像。但它返回 null Intent onActivityResult 。这是我的代码

CaptureImageFromCamera.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, 7);
    }
});

并且onActivityResult

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 7 && resultCode == RESULT_OK) {
        try {
            if(data.getData() == null) {
                bitmap = (Bitmap)data.getExtras().get("data");
            } else {
                bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), data.getData());
            }

            ImageViewHolder.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

我是Android新手,所以请详细解释答案。提前致谢..!

4

4 回答 4

1

通过传递意图来开始您的工作:

startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE), 100);

因此,在从相机捕获图像后,可以通过意图获取有关图像的数据:

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     Uri yourCapturedImage = intent.getData();
 }
于 2017-07-15T06:09:48.650 回答
0

确保您已添加相机权限、在清单中写入权限以及交叉验证运行时权限。

使用您的 Intent 如下所示,并指定将写入捕获图像的路径 -

   intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

   Uri outputUri = Uri.fromFile("path of your directory");
   intent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);

   startActivityForResult(intent, 7);
于 2017-07-15T06:20:18.840 回答
0

Uri imageUri = data.getData()将不再工作..首先bitmapdata这样的:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        val imageBitmap = data.extras.get("data") as Bitmap
        imageView.setImageBitmap(imageBitmap)
    }
}

然后保存位图图像..请在此处查找详细信息:

https://developer.android.com/training/camera/photobasics

于 2020-12-03T05:25:52.517 回答
0

您也可以使用setImageURI()inonActivityResult()代替setImageBitmap()

首先在 android manifest 中添加相机权限。如果您的 android 版本大于 lollipop,那么您需要添加运行时权限,请参阅此处的文档

<uses-permission android:name="android.permission.CAMERA"/>

然后你的代码将是

CaptureImageFromCamera.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, 7);
    }
});

然后setImageUrI()在你的onActivityResult()

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

     switch (requestCode)
        {
            case 7:
                /*case 7 for image results and display image in imageview via Uri*/
                if (resultCode == RESULT_OK)
                {
                    Uri imageUri = data.getData();
                    ImageViewHolder.setImageURI(imageUri);
                }
                return;
           default:
                return;
        }
}
于 2017-07-15T08:15:45.837 回答