java - 使用Android 29下的Camera X ImageCapture.OutputFileOptions.Builder将图像保存到特定文件夹
问题描述
我正在尝试实现 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");
}
}
}
解决方案
推荐阅读
- asp.net - 我想获得选定的单选按钮值
- c# - 即使不使用 RedirectStandardError/RedirectStandardOutput,Process.WaitForExit 也会挂起
- ansible - Ansible 和 Jinja2 变量组合
- c++ - 未找到函数 setenv()?
- java - JPA 数据库连接问题
- spring-boot - 无法连接到 datastax cassandra
- php - PHP Mysql查询优化报告
- spring-mvc - 在角度为 5 的下拉菜单中显示来自远程服务器的文件列表
- bash - Shell 脚本从文件中获取日期部分,然后将其与过去 2 天进行比较
- python - 用 django 创建类似按钮的最佳方法是什么?