参考:
https://developer.android.google.cn/training/camera/photobasics
https://juejin.im/entry/59c9ae4ff265da0658153544
一、基本步骤:
检查权限
检查是否有相机资源
通过FileProvide创建共享文件
启动相机
处理返回的数据
二、代码
1、获取完整照片
private static final int REQUEST_TAKE_PHOTO = 1;
private String currentPhotoPath;
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED){
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
/**
*Notice that the startActivityForResult() method is protected by a condition that *calls resolveActivity(), which returns the first activity component that can handle *the intent. Performing this check is important because if you call *startActivityForResult() using an intent that no app can handle, your app will *crash. So as long as the result is not null, it's safe to use the intent.
*粗略理解为检测是否有相机应用
**/
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
//使用自定义函数创建文件
photoFile = createImageFile();
} catch (IOException ex) {
Log.d(TAG, "文件创建失败");
}
if (photoFile != null) {
//创建共享文件,由于Android7.0行为变更所以使用FileProvide共享文件,
//FileProvide是ContentProvide的子类,所以需要在Manifest中注册
/**
* <provider
* android:name="android.support.v4.content.FileProvider"
* android:authorities="这里填写自己的包名要和getUriForFile()函数中传的一致"
* android:exported="false"
* android:grantUriPermissions="true">
* <meta-data
* android:name="android.support.FILE_PROVIDER_PATHS"
* <!--需要在res文件夹下创建xml文件夹中的file_paths文件-->
* android:resource="@xml/file_paths">
* </meta-data>
* </provider>
*
*file_paths文件内容
*<?xml version="1.0" encoding="utf-8"?>
*<paths xmlns:android="http://schemas.android.com/apk/res/android">
*<external-path name="my_images" path="Android/data/这里填写应用包名/files/Pictures" />
*</paths>
**/
Uri photoURI;
if (Build.VERSION.SDK_INT >= 24){
photoURI = FileProvider.getUriForFile(this,
getPackageName(),
photoFile);
}else {
photoURI = Uri.fromFile(photoFile);
}
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
/**
*创建文件函数
**/
private File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.CHINA).format(new Date());
String imageFileName = "IMG_" + timeStamp + "_";
//获取外部储存路径
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName,
".jpg",
storageDir
);
//保存当前文件的路径
currentPhotoPath = image.getAbsolutePath();
return image;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK){
//处理图片信息处理
}
}
附Android7.0行为变更好文:https://blog.csdn.net/lmj623565791/article/details/72859156
2、获取缩略图
调用系统相机拍照时,如果不传路径,图片默认返回缩略图。
static final int REQUEST_IMAGE_CAPTURE = 1;
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
//继续处理
}
}