首页 > 解决方案 > 在 Android 11 中,我无法使用相机意图获取结果代码 0 如何解决此错误?

问题描述

下面是我的代码

public static Uri launchPhotoChooser(final Fragment fragment) {

文件路径 = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "我的应用程序目录");

    if (!path.exists()) {
        path.mkdir();
    }

    File imageFile = null;
    try {
        imageFile = File.createTempFile("Your file Name", ".jpg", path);
    } catch (IOException e) {
        e.printStackTrace();
    }
    Context context = fragment.getContext();
    Uri uri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".fileProvider", imageFile);
    // Camera.
        final List<Intent> cameraIntents = new ArrayList<Intent>();
        final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        final PackageManager packageManager = fragment.getActivity().getPackageManager();
        final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
        for (ResolveInfo res : listCam)
        {
            final String packageName = res.activityInfo.packageName;
            final Intent intent = new Intent(captureIntent);
            intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
            intent.setPackage(packageName);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
            cameraIntents.add(intent);
        }

        // Filesystem.
        Intent galleryIntent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        galleryIntent.setType("image/*");
        galleryIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());

        // Chooser of filesystem options.
        final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");

        // Add the camera options.
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()]));
        fragment.startActivityForResult(chooserIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION), REQUEST_CODE_PICTURE);
        return uri;
}

标签: androidandroid-intentandroid-cameraandroid-11scoped-storage

解决方案


推荐阅读