使用registerForActivityResult访问相机时出现ActivityNotFoundException



My API 28应用程序在尝试使用registerForActivityResult使用相机(在Emulator和实际设备中(拍摄照片时抛出此ActivityNotFoundException:"未找到处理Intent{act=android.media.action.IMAGE_CAPTURE typ=images.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时,该作业运行良好,所以我认为权限、路径和依赖关系都很好。。。

删除setType()调用。并且,考虑用ActivityResultContracts.TakePicture()替换所有这些。

相关内容

  • 没有找到相关文章

最新更新