简体   繁体   English

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

[英]Image captured from camera not displaying in imageview android

I have a problem, that when i have taken an image from camera, image not displaying in imageview. 我有一个问题,当我从相机拍摄图像时,图像不在imageview中显示。

I created the code by referring the following link 我通过引用以下链接创建了代码

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

I am posting my code, please have a look, 我发布了我的代码,请看看,

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

The image captured is storing in SDcard. 捕获的图像存储在SD卡中。 But not showing in imageview. 但没有在imageview中显示。

Where i have gone wrong. 我哪里出错了。 I have tried a lot. 我已经尝试了很多。 But no result. 但没有结果。 Is there any way to solve this issue. 有没有办法解决这个问题。

this works for me: 这对我有用:

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

You don't need to create the temp file, just put the Uri in the intent. 您不需要创建临时文件,只需将Uri放入intent即可。 After the capture, check the file existence of that Uri. 捕获后,检查该Uri的文件是否存在。 If it exists, capture has been done successfully. 如果存在,则捕获已成功完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM