2016-11-22 88 views
-1

我使用以下代码从android相机拍摄图片,拍摄照片后onActivityResult被调用,数据参数等于NULL。为什么我在onActivityResult的数据参数中为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); 
    } 
} 
+0

你有清单许可吗? – JoCuTo

+0

在Manifest中添加权限,如果您使用的是Android版本6,则必须获得@ S.L的运行时权限。 –

回答

0

报价the documentation for ACTION_IMAGE_CAPTURE

调用者可以通过一个额外的EXTRA_OUTPUT为了控制这个图像将被写入。如果EXTRA_OUTPUT不存在,则在额外字段中将小图像作为位图对象返回。这对于只需要小图像的应用程序很有用。如果EXTRA_OUTPUT存在,则全尺寸图像将被写入EXTRA_OUTPUT的Uri值。

您提供了EXTRA_OUTPUT。您的照片应写入您在EXTRA_OUTPUT中提供的位置。你的data额外的回应应该是null

+0

非常感谢您的回复。 –

相关问题