第一种方式,ContentResolver在外部pictures目录下预先创建空的uri图片,拍完照再写入uri,Android10以上只需相册权限,Android10以下需要相册权限以及读写权限
/**
* 创建存入外部的公有媒体的Uri
* MediaStore.Images.Media.EXTERNAL_CONTENT_URI
* 访问路径 content://media/external/images/media/36902
* 路径用于拍照后存储在sd卡下的公共媒体picture
* @return
*/
public static Uri createMediaImageUri(Context context){
try {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "IMG_" + timeStamp;
// File storageDir = mContext.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
// File image = File.createTempFile(imageFileName, ".jpg", storageDir);
// Uri insertUri = getUriFromFile(mContext,image);
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DESCRIPTION, imageFileName);
values.put(MediaStore.Images.Media.DISPLAY_NAME, imageFileName);
values.put(MediaStore.Images.Media.DATE_MODIFIED, System.currentTimeMillis() / 1000);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q){
// values.put(MediaStore.Images.Media.IS_PENDING, 0);
values.put(MediaStore.Images.Media.RELATIVE_PATH, "Pictures/" + AppConfig.getAppName());
}
ContentResolver resolver = context.getContentResolver();
Uri external = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
return resolver.insert(external, values);
}catch (Exception e){
e.printStackTrace();
return null;
}
}
//开启相机,拍完照写入uri,onActivityResult回调处理uri即可
private void startCamera(){
mTakePhotoUri = PathUtils.createMediaImageUri(this);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(intent.resolveActivity(this.getPackageManager()) != null){
intent.putExtra(MediaStore.EXTRA_OUTPUT, mTakePhotoUri);
startActivityForResult(intent, REQUEST_CODE_TAKE_PICTURE);
}
}
第二种方式,使用File在外部pictures新建文件名以及路径,并没有预先创建好文件,然后将file转为uri传入相机,拍完照后在onActivityResult使用MediaScannerConnection刷新相册,需要相机权限以及读写权限
private File imageStorageDir;
private String currentImageFilePath;
private void startCamera(){
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
Intent chooserIntent = Intent.createChooser(i, "");
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
if (imageStorageDir == null) {
imageStorageDir = new
File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),AppConfig.getAppName());
}
if (!imageStorageDir.exists()) {
imageStorageDir.mkdirs();
}
File file = new File(imageStorageDir + File.separator + "IMG_" + timeStamp + ".jpg");
currentImageFilePath = file.getAbsolutePath();
mCapturedImageURI = Uri.fromFile(file);
if (Build.VERSION.SDK_INT >= 24) {mCapturedImageURI = FileProvider.getUriForFile(this, getBaseContext().getString(R.string.file_provider_authorities), file);
chooserIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
chooserIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
final Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
final Intent photoIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Parcelable[]{captureIntent, photoIntent});
startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
MediaScannerConnection.scanFile(this,new String[]{currentImageFilePath}, new String[]{"image/jpg"}, null);
}