每当我使用ACTION_IMAGE_CAPTURE Intent启动相机时,都会保存两张照片。一是我刚拍的照片。另一张是空白照片。重新启动后,空白照片将消失。
例如,真实照片的文件地址为:“ / storage / emulated / 0 / DCIM / raspberry / SideBySide4_10_22_2018_09:40:26.png”。空白将具有文件地址:“ / external_files / DCIM / raspberry / SideBySide4_10_22_2018_09:40:26.png”。但是在设备文件资源管理器中没有诸如external_files这样的文件夹。
这是我创建文件地址以将拍摄的照片保存到的方式:
private File createImageFile() throws IOException{
String timeStamp=new SimpleDateFormat("MM_dd_yyyy_HH:mm:ss").format(new Date());
String imageFileName="SideBySide4_"+timeStamp+"_";
File storageDir=new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "Camera");
File image=File.createTempFile(
imageFileName
,".jpeg"
,storageDir
);
mCurrentPhotoPath="file:"+image.getAbsolutePath();
return image;
}
这是我打开相机并告诉它保存到Uri的方式:
private void dispatchTakePictureIntent() throws IOException{
Intent takePictureIntent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//check if there is a camera activity
if(takePictureIntent.resolveActivity(getPackageManager())!=null){
File photoFile=null;
try{
photoFile=createImageFile();
}catch(IOException e){
return;
}
if(photoFile!=null){
Uri photoUri= FileProvider.getUriForFile(this
,BuildConfig.APPLICATION_ID+".provider"
,photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(takePictureIntent,REQUEST_TAKE_PHOTO);
}
}
}
我告诉MediaScanner扫描拍摄的新照片:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==REQUEST_TAKE_PHOTO && resultCode==RESULT_OK){
Uri imageUri=Uri.parse(mCurrentPhotoPath);
MediaScannerConnection.scanFile(this
,new String[]{imageUri.getPath()}
,null
,new MediaScannerConnection.OnScanCompletedListener(){
public void onScanCompleted(String path, Uri uri){
}
});
}
}