相机意图结果的预期 Parcelable

Expected Parcelable for result for camera Intent

我正在制作相机意图并使用 activity 结果存储快照,这是我的代码:

File imageFolder=new File(context.getExternalCacheDir(),"Cam/" + form);
imageFolder.mkdirs();
String random= UUID.randomUUID().toString();
String filename = random + ".jpg";
TakenImage =  imageFolder + "/" + filename;
Intent camera=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
camera.putExtra(MediaStore.EXTRA_OUTPUT,TakenImage);
activityResultCamera.launch(camera);

但是我在最后一行收到这个错误:

Key output expected Parcelable but value was a java.lang.String. The default value was returned.

Attempt to cast generated internal exception:

java.lang.ClassCastException: java.lang.String cannot be cast to android.os.Parcelable

Camera是一个Intent,我也把ActivityResultLauncher声明为Intent

ActivityResultLauncher<Intent> activityResultCamera = registerForActivityResult(...

所以,我做错了什么?

EXTRA_OUTPUT 需要:

  • 一个Uri...
  • 方案content

您有:

  • 一个String...
  • 这是其他应用无法访问的文件系统路径 Android 10+

使用 FileProvider 获取指向您所需位置的 Uri,并在 EXTRA_OUTPUT 中使用该 Uri