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;
}

静态最终int REQUEST_TAKE_PHOTO = 1;
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);
        }
    }
}

main.xml
<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" />

嗨,我想拍照并保存到我创建的“MyImages”文件中,我想查看照片imageview。我可以拍照并保存,但是无法使用imageview查看。 putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(photoFile));由于Uri.fromFile(photoFile),data为null。请帮忙!

最佳答案

如果指定了MediaStore.EXTRA_OUTPUT,则所拍摄的图像将被写入该路径,并且不会将任何数据提供给o​​nActivityResult。您可以从指定的位置读取图像。

在此处查看另一个已解决的相同问题:Android Camera:数据 Intent 返回null

关于Android捕获照片putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(photoFile));数据为空,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28638958/

10-09 09:39