Android捕获照片putExtra(MediaStore.EXTRA_输出,Uri.fromFile(photoFile));数据为空

Android捕获照片putExtra(MediaStore.EXTRA_输出,Uri.fromFile(photoFile));数据为空,android,nullpointerexception,camera,Android,Nullpointerexception,Camera,静态最终int请求\u拍摄\u照片=1 private File createImageFile() throws IOException { // Create an image file name String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); String imageFileName = "JPEG_" + timeStamp + "_"; Fil

静态最终int请求\u拍摄\u照片=1

 private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(
            "MyImages");
    storageDir.mkdirs();
  //  File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
    File image = File.createTempFile(
        imageFileName,  /* prefix */
        ".jpg",         /* suffix */
        storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;
}
main.xml

private void dispatchTakePictureIntent() {
     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
         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) {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(photoFile));
            //takePictureIntent = getIntent().putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {  
        if(data != null) {
            Bitmap photo = (Bitmap) data.getExtras().get("data"); 
            imageView.setImageBitmap(photo);
        }
    }  
}


您好,我想拍照并保存到我创建的文件“MyImages”,我想查看照片图像视图。我可以拍照并保存它,但我不能用imageview查看。putExtra(MediaStore.EXTRA_输出,Uri.fromFile(photoFile));由于Uri.fromFile(photoFile),数据为空。请帮忙

如果指定了MediaStore.EXTRA_输出,则拍摄的图像将写入该路径,并且不会向onActivityResult提供任何数据。您可以从指定的内容读取图像


请参阅此处解决的另一个相同问题:Android摄像头:数据意图返回空值;另见Okey。非常感谢你!
<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.59"
    android:text="File Uri" />


<ImageView
    android:id="@+id/imageView1"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_weight="0.52"
    android:maxHeight="@dimen/max_image_height"
    android:src="@drawable/ic_launcher" />