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

标签 android camera

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

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

http://developer.android.com/training/camera/photobasics.html

我贴出我的代码,请看,

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 中。

我哪里出错了。我已经尝试了很多。但没有结果。有什么办法可以解决这个问题。

最佳答案

这对我有用:

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

关于android - 从相机捕获的图像未显示在 imageview android 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33886073/

相关文章:

ios - 使用 UIView 水平裁剪图像

java - 使用 double 作为比较对 JSONArray 进行排序违反了约定?

java - 如何在recyclerview中的复选框上进行单选?

java - 使用 Navigation 包中的 startActivity

java - 检测所有摄像头android

android - 您的设备似乎不支持相机

android - 如何不将照片保存到本地?

ios - 是否可以在 AVCaptureVideoDataOutput 中设置 AVCaptureSessionPresetPhoto?

Android 应用内订阅测试

android - ionic 构建错误 : Could not resolve com. android.tools.build :gradle:1. 5.0