为什么在onActivityResult的数据参数中为null [英] Why do I get null in data parmeter of onActivityResult
问题描述
在使用数据参数等于NULL的pic onActivityResult拍照后,我使用以下代码从android相机拍照.
I use the following code to take picture from the android camera, after taking the pic onActivityResult is called with data parameter equals NULL.
代码-
public void getpic(View v){
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
//...
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.myfirstapp.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);//REQUEST_TAKE_PHOTO
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
mImageView.setImageBitmap(imageBitmap);
}
}
推荐答案
Quoting the documentation for ACTION_IMAGE_CAPTURE
:
调用方可以传递一个额外的EXTRA_OUTPUT来控制将图像写入何处.如果不存在EXTRA_OUTPUT,则会在Extra字段中将一个小型图像作为Bitmap对象返回.这对于只需要较小图像的应用程序很有用.如果存在EXTRA_OUTPUT,则将完整尺寸的图像写入EXTRA_OUTPUT的Uri值.
The caller may pass an extra EXTRA_OUTPUT to control where this image will be written. If the EXTRA_OUTPUT is not present, then a small sized image is returned as a Bitmap object in the extra field. This is useful for applications that only need a small image. If the EXTRA_OUTPUT is present, then the full-sized image will be written to the Uri value of EXTRA_OUTPUT.
您提供了 EXTRA_OUTPUT
.您的照片应写到您在 EXTRA_OUTPUT
中提供的位置.您在响应中额外添加的 data
应该为 null
.
You provided EXTRA_OUTPUT
. Your photo should be written to the location you provided in EXTRA_OUTPUT
. Your data
extra on the response should be null
.
这篇关于为什么在onActivityResult的数据参数中为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!