简体   繁体   English

使用 registerForActivityResult 访问相机时出现 ActivityNotFoundException

[英]ActivityNotFoundException when accessing the camera using registerForActivityResult

My API 28 application throws this ActivityNotFoundException when trying to capture photos with the camera (both in the Emulator as well as with a real device) using registerForActivityResult: "No Activity found to handle Intent { act=android.media.action.IMAGE_CAPTURE typ=image/jpg flg=0x3 clip={text/uri-list U:content://... "我的 API 28 应用程序在尝试使用 registerForActivityResult 使用相机(在模拟器和真实设备中)拍摄照片时抛出此 ActivityNotFoundException:“没有找到处理 Intent 的活动 { act=android.media.action.IMAGE_CAPTURE typ= image/jpg flg=0x3 clip={text/uri-list U:content://..."

`public class IntentHelper { `公共class 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;
}

}` }`

I call the handleIntent method in an object of this custom IntentHelper class from a fragment, triggered by a click event.我从一个片段调用这个自定义 IntentHelper class 的 object 中的 handleIntent 方法,由点击事件触发。 The ActivityResultRegistry parameter passed in the constructor is retrieved in that fragment from the main activity.构造函数中传递的 ActivityResultRegistry 参数在该片段中从主活动中检索。

The application does create empty files in the corresponding folder while he device camera does not open.当设备相机未打开时,应用程序会在相应文件夹中创建空文件。 The job worked fine before when I was using the deprecated startActivityForResult, so I assume that permissions, paths and dependencies are fine...在我使用已弃用的 startActivityForResult 之前,这项工作工作得很好,所以我认为权限、路径和依赖项都很好......

Remove the setType() call.删除setType()调用。 And, consider replacing all of this with ActivityResultContracts.TakePicture() .并且,考虑用ActivityResultContracts.TakePicture()替换所有这些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在片段中使用 registerForActivityResult 时,永远不会调用回调 - When using registerForActivityResult in a fragment the callback is never called 与Unity使用Facebook插件时的ActivityNotFoundException - ActivityNotFoundException when using Facebook plugin with Unity Android 应用程序在使用 registerForActivityResult 启动另一个活动时退出 - Android app exits when launch another activity using registerForActivityResult 访问相机时崩溃? - Crash when accessing camera? 如何使用 registerForActivityResult 启动相机? - How do I use registerForActivityResult to launch Camera? 使用ContentProvider打开文件时出现android.content.ActivityNotFoundException - android.content.ActivityNotFoundException when opening file using ContentProvider 使用隐式意图时,如何解决我代码中的ActivityNotFoundException? - How to resolve ActivityNotFoundException in my code when using implicit intents? 打开浏览器时出现ActivityNotFoundException - ActivityNotFoundException when opening the browser 使用Camcorder的ActivityNotFoundException。(android.media.action.VIDEO_CAMERA) - ActivityNotFoundException wnen using Camcorder.(android.media.action.VIDEO_CAMERA ) 使用ZXing时出现ActivityNotFoundException - ActivityNotFoundException while using ZXing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM