Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从相机捕获的图像未显示在imageview android中_Android_Camera - Fatal编程技术网

从相机捕获的图像未显示在imageview android中

从相机捕获的图像未显示在imageview android中,android,camera,Android,Camera,我有一个问题,当我从相机拍摄图像时,图像没有显示在imageview中 我通过引用以下链接创建了代码 我正在发布我的代码,请看一下 public void takeImage(View v) { Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (takePictureIntent.resolveActivity(getPackageManager()) != null) {

我有一个问题,当我从相机拍摄图像时,图像没有显示在imageview中

我通过引用以下链接创建了代码

我正在发布我的代码,请看一下

public  void takeImage(View v) {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        File photoFile = null;

        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
        }

        if (photoFile != null) {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }
}

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

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

    galleryAddPic();

    return image;
}

private void galleryAddPic() {
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    File f = new File(mCurrentPhotoPath);
    Uri contentUri = Uri.fromFile(f);
    mediaScanIntent.setData(contentUri);
    this.sendBroadcast(mediaScanIntent);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    try {
        if (requestCode == REQUEST_IMAGE_CAPTURE) {
            Bundle extras = data.getExtras();
            Bitmap imageBitmap = (Bitmap) extras.get("data");
            mImageView.setImageBitmap(imageBitmap);
        }
    }catch (Exception e) {
        Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG).show();
    }
}
捕获的图像存储在SD卡中。但不显示在imageview中


我错在哪里了。我试了很多。但没有结果。有没有办法解决这个问题。

您不需要创建临时文件,只需将Uri放在意图中即可。捕获后,检查该Uri的文件是否存在。如果存在,则已成功完成捕获。

这对我很有用:

Bitmap myBitmap = BitmapFactory.decodeFile(imagePath);
image.setImageBitmap(myBitmap);

第一次使用这个库很难,但每次都很容易使用。您是否提供了sd卡中图像到图像视图的正确路径?您是否在图像位图中获取了一些值?这里-
Bitmap-imageBitmap=(位图)extras.get(“数据”)
@sud:它总是显示
出错的消息。甚至不打算尝试block.toast替换为-
toast.makeText(this,e,toast.LENGTH_SHORT).show()它将显示错误到底是什么?它将显示什么错误?