本篇文章内容来自:
https://blog.csdn.net/u011277123/article/details/52677581
目录
- 直接调取相机拍照,获取到缩略图
- 调取相机拍照,保存全尺寸照片
1.直接调取相机拍照,获取到缩略图
直接调取相机拍照,无需任何权限,但是只能获取到缩略图
第一步 声明照相机权限
照相机为危险权限
<uses-permission android:name="android.permission.CAMERA" />
@OnClick(R.id.btn_camera)
public void onViewClicked() {
if (ContextCompat.checkSelfPermission(CameraActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
//没有授权,则申请授权
//ActivityCompat.requestPermissions() 参数一:context 参数二:申请的权限名数组 参数三:请求码,要求唯一值
ActivityCompat.requestPermissions(CameraActivity.this, new String[]{Manifest.permission.CAMERA}, PERMISSION_TAKE_CAMERA);
} else {
takePhoto();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case PERMISSION_TAKE_CAMERA:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
takePhoto();
} else {
Toast.makeText(CameraActivity.this, "you denied", Toast.LENGTH_SHORT).show();
}
break;
}
}
第二步 调用照相机 获取缩略图
private void takePhoto() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null) {//判断是否有相机应用
startActivityForResult(intent, REQ_CAMERA);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CAMERA:
if (resultCode != Activity.RESULT_OK) return;
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
ivShow.setImageBitmap(imageBitmap);
break;
}
}
2. 调取相机拍照,保存全尺寸照片
第一步 声明照相机权限+存储权限
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
@OnClick({R.id.btn_camera_full})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.btn_camera_full:
List<String> permissionList = new ArrayList<>();
if (ContextCompat.checkSelfPermission(CameraActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
permissionList.add(Manifest.permission.READ_PHONE_STATE);
}
if (ContextCompat.checkSelfPermission(CameraActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (!permissionList.isEmpty()) {
String[] permissions = permissionList.toArray(new String[permissionList.size()]);
ActivityCompat.requestPermissions(CameraActivity.this, permissions, PERMISSION_TAKE_CAMERA_FULL);
} else {
takeFullPhoto();
}
break;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case PERMISSION_TAKE_CAMERA_FULL:
if (grantResults.length > 0) {
for (int result : grantResults) {
if (result != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(CameraActivity.this, "you denied some", Toast.LENGTH_SHORT).show();
return;
}
}
takeFullPhoto();
} else {
Toast.makeText(CameraActivity.this, "发生未知错误", Toast.LENGTH_SHORT).show();
}
}
}
第二步 调用照相机 获取全尺寸图片
要点:Android N 以上要使用FileProvider
private void takeFullPhoto() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null) {//判断是否有相机应用
photoFile = null;
try {
photoFile = FileUtil.createImageFile(CameraActivity.this);
} catch (IOException e) {
e.printStackTrace();
}
if (photoFile != null) {
//判断是否是AndroidN以及更高的版本
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri photoURI = FileProvider.getUriForFile(CameraActivity.this, "com.example.android.fileprovider", photoFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
} else {
Uri uri = Uri.fromFile(photoFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
}
startActivityForResult(intent, REQUEST_TAKE_PHOTO_FULL);
}
}
}
public class FileUtil {
public static File createImageFile(Context context) throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.CHINA).format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
//.getExternalFilesDir()方法可以获取到 SDCard/Android/data/你的应用的包名/files/ 目录,一般放一些长时间保存的数据
File storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
//创建临时文件,文件前缀不能少于三个字符,后缀如果为空默认未".tmp"
File image = File.createTempFile(
imageFileName, /* 前缀 */
".jpg", /* 后缀 */
storageDir /* 文件夹 */
);
return image;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUEST_TAKE_PHOTO_FULL:
if (resultCode != Activity.RESULT_OK) return;
ivShow.setImageBitmap(BitmapFactory.decodeFile(photoFile.getAbsolutePath()));
break;
}
}
部分手机可能出现不显示照片的bug
解决办法来自
BitmapFactory解码图片不显示(项目中的 bug)
将显示图片的ivShow.setImageBitmap(BitmapFactory.decodeFile(photoFile.getAbsolutePath()));
改为:
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 4;
Bitmap bitmap = BitmapFactory.decodeFile(photoFile.getAbsolutePath(), opts);
ivShow.setImageBitmap(bitmap);
网友评论