[英]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.