调用摄像头进行拍照获取图片

这里为了得到清晰的图像,不适用返回的data,因为这个得到的是缩略图。这里的做法是在调用摄像头之前指定图片的保存位置,然后通过调用结果函数和指定的图片存储路径,从文件中获取图片。

启动摄像头的代码:

1 intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
2             File file = new File(Environment.getExternalStorageDirectory()
3                     + "/tempImage.jpg");
4             imagePath = file.getAbsolutePath();
5             Uri outUri = Uri.fromFile(file);//获取文件的uri
6             intent.putExtra(MediaStore.EXTRA_OUTPUT, outUri);//指定图像存储位置
7             startActivityForResult(intent, 2);

调用摄像头的话需要增加相应的权限,还有对文件的读写权限:

1 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
2 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
3 <uses-permission android:name="android.permission.CAMERA" />

然后通过onActivityResult函数将图像从文件中读取并显示出来:

1 ImageView image = new ImageView(this);
2 //这里的imgepath就是刚才设置的保存图像的文件
3 image.setImageBitmap(BitmapFactory.decodeFile(imagePath));
原文地址:https://www.cnblogs.com/guanking19/p/4585628.html