|
@@ -557,29 +557,37 @@ public class MainActivity extends BaseActivity {
|
|
|
cameraProvider.bindToLifecycle((LifecycleOwner) this, cameraSelector, preview, imageCapture);
|
|
|
|
|
|
// 通过 ContentValues 对象设置文件名和文件类型
|
|
|
- Log.e("-------->","222------>savePath="+savePath);
|
|
|
ContentValues contentValues = new ContentValues();
|
|
|
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, savePath);
|
|
|
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg");
|
|
|
|
|
|
+ File dir = new File(Environment.getExternalStorageDirectory() + "/camera/");
|
|
|
+ if (!dir.exists()) {
|
|
|
+ dir.mkdirs();
|
|
|
+ }
|
|
|
+ //创建文件
|
|
|
+ File file = new File(dir,savePath);
|
|
|
+ ImageCapture.OutputFileOptions outputFileOptions = new ImageCapture.OutputFileOptions.Builder(file).build();
|
|
|
imageCapture.takePicture(
|
|
|
// 第一个参数 OutputFileOptions 指定了照片保存的位置和格式等信息。
|
|
|
- new ImageCapture.OutputFileOptions.Builder(
|
|
|
- getContentResolver(),
|
|
|
- MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
|
|
|
- contentValues
|
|
|
- ).build(),
|
|
|
+// new ImageCapture.OutputFileOptions.Builder(
|
|
|
+// getContentResolver(),
|
|
|
+// MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
|
|
|
+// contentValues
|
|
|
+// ).build(),
|
|
|
+ outputFileOptions,
|
|
|
// 第二个参数 Executor 指定了保存照片时要运行的线程。
|
|
|
ContextCompat.getMainExecutor(this),
|
|
|
// 第三个参数 OnImageSavedCallback 指定了保存照片完成后的回调函数,可以在其中进行一些提示或其他操作。
|
|
|
new ImageCapture.OnImageSavedCallback() {
|
|
|
+ @SuppressLint("RestrictedApi")
|
|
|
@Override
|
|
|
public void onImageSaved(@NonNull ImageCapture.OutputFileResults outputFileResults) {
|
|
|
Log.e("-------->","333------>savePath="+outputFileResults.getSavedUri());
|
|
|
- Log.e("-------->","444------>savePath="+outputFileResults.getSavedUri().getPath());
|
|
|
+ Log.e("-------->","444------>savePath="+outputFileOptions.getFile().getAbsolutePath());
|
|
|
Log.e("-------->","------>保存成功");
|
|
|
// Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
|
|
|
- Log.e("-------->","------->path="+getImagePath(savePath));
|
|
|
+ Log.e("-------->","------->path="+file.getAbsolutePath());
|
|
|
// Toast.makeText(MainActivity.this, "path="+savePath, Toast.LENGTH_SHORT).show();
|
|
|
// Toast.makeText(MainActivity.this, "path="+getImagePath(savePath), Toast.LENGTH_SHORT).show();
|
|
|
// new Handler().postDelayed(new Runnable() {
|