我的 API 28 应用程序在尝试使用 registerForActivityResult 使用相机(在模拟器和真实设备中)拍摄照片时抛出此 ActivityNotFoundException:“未找到处理 Intent { act=android.media.action.IMAGE_CAPTURE typ= 的活动image/jpg flg=0x3 clip={text/uri-list U:content://..."
`公共类 IntentHelper {
private static final String AUTHORITY = BuildConfig.APPLICATION_ID + ".fileprovider";
File output = null;
private final ActivityResultRegistry mRegistry;
private ActivityResultLauncher<Intent> takePhotoActivityResultLauncher;
private final Context context;
private final Activity activity;
private final FragmentOperationBinding binding;
public IntentHelper(Context context, Activity activity, FragmentOperationBinding binding, ActivityResultRegistry registry) {
this.mRegistry = registry;
this.context = context;
this.activity = activity;
this.binding = binding;
}
public void handleIntent() {
setUpLauncher();
createIntent();
}
private void setUpLauncher() {
takePhotoActivityResultLauncher = mRegistry.register("key",
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK) {
final int THUMBSIZE = 64;
Bitmap ThumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(output.getAbsolutePath()),
THUMBSIZE, THUMBSIZE);
binding.photoThumbnail.setImageBitmap(ThumbImage);
Intent returnToCallingActivityIntent = new Intent(context, OperationFragment.class);
Uri outputUri = FileProvider.getUriForFile(context, AUTHORITY, output);
returnToCallingActivityIntent.setDataAndType(outputUri, "image/jpeg");
returnToCallingActivityIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
}
});
}
private void createIntent() {//Create Intent
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(activity.getPackageManager()) != null) {
// Create the File where the photo should go
try {
output = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
Toast.makeText(context, ex.getMessage(), Toast.LENGTH_LONG).show();
}
takePictureIntent.setType("image/jpg");
Uri photoURI = FileProvider.getUriForFile(context, AUTHORITY, output);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
binding.photoThumbnail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Launch activity to get result
takePhotoActivityResultLauncher.launch(takePictureIntent);
}
});
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = binding.getOperation().getIdentifier() + "_" + timeStamp;
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS + "/operationtimerecord");
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
String currentPhotoPath = image.getAbsolutePath();
return image;
}
}`
我从一个片段调用这个自定义 IntentHelper 类的对象中的 handleIntent 方法,由点击事件触发。构造函数中传递的 ActivityResultRegistry 参数在该片段中从主活动中检索。
当设备相机未打开时,应用程序会在相应文件夹中创建空文件。在我使用已弃用的 startActivityForResult 之前,这项工作工作得很好,所以我认为权限、路径和依赖项都很好......