Android的显示图像从相机
我尝试使用下面的代码拍照与摄像头,并在ImageView
Android的显示图像从相机
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();
}
}}}
问题显示这是它不显示图片
它拍摄照片给它一个文件名并将其保存在照片目录中。
当它到达的onActivityResult outputFileUri.toString()提供了以下
“文件:/storage/emulated/0/Pictures/03-09-2014-06-53-04.jpg”
当我检查的图片目录中的图片是存在的,拼写正确
,但是当它进入imgFile.exists如果它说,它不存在,声明和敬酒的干杯其他
任何想法,我错了吗?
任何帮助赞赏
马克
使用此代码:
if(imgFile!=null){
Bitmap myBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imgFile.toString());
ImageView myImage = (ImageView) findViewById(R.id.mImageView);
myImage.setImageBitmap(myBitmap);
}
我试过这个,但getbitmap行中出现错误,说“MediaStore.Images.Media类型中的方法getBitmap(ContentResolver,Uri)不适用于参数(ContentResolver,String) “ – user3422687 2014-09-03 17:34:36
谢谢我工作了什么是缺少我需要添加this.imageView =(ImageView)this.findViewById(R.id.mImageView); – user3422687 2014-09-03 22:52:01
在onActivityResult
如果你的情况是RESULT_OK
,你的参数data
就已经具有捕获的位图。
所以,你可以试试这个:
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
希望这有助于。
看看http://stackoverflow.com/questions/5991319/capture-image-from-camera-and-display-in-activity – 2014-09-03 06:09:56
你'data'意志已经包含图像。因此,只需尝试“位图照片=(位图)data.getExtras()。get(”data“); imageView.setImageBitmap(photo);' – 2014-09-03 06:11:46