java使用Camera X ImageCapture将图像保存到特定文件夹。OutputFileOptions。安卓29以下的构建器
我正在尝试实现Camera X应用程序。我现在面临的问题是,我不知道如何使用ImageCapture.OutputFileOptions.Builder
将下面的图像保存到特定的文件夹中
我现在的代码如下。我需要采取其他方式吗?或者我也可以这样做
private void capturePhoto() {
showProgress(true);
long currentTime = System.currentTimeMillis();
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
contentValues.put(MediaStore.Images.Media.DISPLAY_NAME, makeImageName(currentTime));
if (Build.VERSION.SDK_INT >= 29) {
contentValues.put(MediaStore.Images.Media.DATE_TAKEN, currentTime);
contentValues.put(MediaStore.Images.Media.RELATIVE_PATH,
Environment.DIRECTORY_PICTURES + "/" + FOLDER_IMAGES);
} else {
// Todo ( Something equivalent to RELATIVE_PATH)
}
ImageCapture.OutputFileOptions options = new ImageCapture.OutputFileOptions.Builder(
getContentResolver(), MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues).build();
imageCapture.takePicture(options,
ContextCompat.getMainExecutor(this),
new ImageCapture.OnImageSavedCallback() {
@Override
public void onImageSaved(@NonNull ImageCapture.OutputFileResults results) {
ToastUtility.successToast(getApplicationContext(),
"Photo Capture Successfully");
showProgress(false);
}
@Override
public void onError(@NonNull ImageCaptureException exception) {
ToastUtility.errorToast(getApplicationContext(),
"Photo Couldn't Capture");
showProgress(false);
}
});
}
更新#1
通过下面的代码解决了这个问题
if (Build.VERSION.SDK_INT >= 29) {
contentValues.put(MediaStore.Images.Media.DATE_TAKEN, currentTime);
contentValues.put(MediaStore.Images.Media.RELATIVE_PATH,
Environment.DIRECTORY_PICTURES + "/" + FOLDER_IMAGES);
} else {
createFolderIfNotExist();
String path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES) + "/" + FOLDER_IMAGES + "/" + imageDisplayName;
contentValues.put(MediaStore.Images.Media.DATA, path);
}
private void createFolderIfNotExist() {
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES) + "/" + FOLDER_IMAGES);
if (!file.exists()) {
if (!file.mkdir()) {
Log.d(TAG, "Folder Create -> Failure");
} else {
Log.d(TAG, "Folder Create -> Success");
}
}
}
共 (0) 个答案