简体   繁体   English

Android从相机在imageview中显示图像

[英]android display image in imageview from camera

I am trying to use the following code to take a picture with the camera and display it in a ImageView 我正在尝试使用以下代码用相机拍照并在ImageView显示

public void takepic(View view) {

    String timeStamp = new SimpleDateFormat("dd-MM-yyyy-HH-mm-ss").format(new Date());
    String imageFileName = timeStamp + ".jpg";
    TextView detail = (TextView)findViewById(R.id.textView1);
    detail.setText(imageFileName); 
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);    
        File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
            String name = imageFileName;


            File file = new File(path, name);
            outputFileUri = Uri.fromFile(file);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
            startActivityForResult(intent, TAKE_PICTURE); 


}

static final int REQUEST_IMAGE_CAPTURE = 1;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    switch(requestCode) {

    case 3:
        if (resultCode == RESULT_OK){

                File imgFile = new  File(outputFileUri.toString());

                TextView detail = (TextView)findViewById(R.id.textView1);
                detail.setText(imgFile.toString()); 

            if(imgFile.exists()){

                Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

                ImageView myImage = (ImageView) findViewById(R.id.mImageView);
                myImage.setImageBitmap(myBitmap);

            }
            else{
                Toast.makeText(getBaseContext(), "Doesnt Exist", Toast.LENGTH_LONG).show();

            }
        }}}

Problem is its not displaying the picture 问题是它不显示图片

It takes the picture gives it a filename and saves it in the pictures directory. 拍摄图片时会给它一个文件名,并将其保存在图片目录中。

when it gets to the onActivityResult outputFileUri.toString() gives the following 当它到达onActivityResult时,outputFileUri.toString()给出以下内容

"file:/storage/emulated/0/Pictures/03-09-2014-06-53-04.jpg" “文件:/storage/emulated/0/Pictures/03-09-2014-06-53-04.jpg”

when i check the pictures directory the picture is there and is spelled correctly 当我检查图片目录时,图片在那里并且拼写正确

but when it goes to the imgFile.exists if statement it says it doesn't exist and toasts the else toast 但是当转到imgFile.exists if语句时,它说它不存在,并敬酒else敬酒

Any ideas where i'm going wrong? 任何想法,我要去哪里错了?

any help appreciated 任何帮助表示赞赏

Mark 标记

In onActivityResult if your case is RESULT_OK , your parameter data would already be having the captured bitmap. onActivityResult如果情况是RESULT_OK ,则参数data将已经具有捕获的位图。

So you can try this: 因此,您可以尝试以下操作:

Bitmap photo = (Bitmap) data.getExtras().get("data"); 
imageView.setImageBitmap(photo);

Hope this helps. 希望这可以帮助。

use this code: 使用此代码:

 if(imgFile!=null){


Bitmap myBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imgFile.toString());
 ImageView myImage = (ImageView) findViewById(R.id.mImageView);
                myImage.setImageBitmap(myBitmap);

}

暂无
暂无

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

相关问题 从 Imageview 中的摄像头和显示器捕获图像 - Capture Image from Camera and Display in Imageview 相机图像没有保存在SD卡中,也没有显示在Android前置摄像头的imageview中 - Camera image is not saving in sdcard and does not display in imageview in front camera in android 从相机获取图像或从图库上传并在 ImageView (Android Studio) 中显示后,保存图像为 SharedPreferences - Save image is SharedPreferences after Image is get from camera or upload from gallery and display at ImageView (Android Studio) 从相机获取图像到ImageView Android - Get Image from camera into ImageView Android 从相机捕获的图像不在imageview android中显示 - Image captured from camera not displaying in imageview android Android:使用相机将图像显示到ImageView并保存到图库 - Android : Use Camera to display image to ImageView and Save to Gallery 如何调整从相机捕获的图像大小并显示在imageview上 - How to resize image that captured from camera and display on imageview ImageView不显示从手机摄像头或照片库拍摄的图像 - ImageView does not display image taken from phone camera or photo gallery 如何在图库中的imageView上设置图像以及在Android中由相机拍摄的图像? - How to set an image on imageView from the gallery and image taken by camera in Android? 如何在android imageview中显示数据库中的图像 - How to display the image from database in android imageview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM