2

我无法根据他们在选择器意图中选择的内容来请求用户许可。目前,我的程序是在选择器意图出现之前请求权限。如何在他们选择意图后以请求权限的方式实现。下面的代码是先请求相机权限或存储权限,然后只允许用户选择他们想要从相机或图库中拍照。

Dexter.checkPermissions(new MultiplePermissionsListener() {

                Intent chooserIntent = null;
                List<Intent> intentList = new ArrayList<>();

                Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                @Override
                public void onPermissionsChecked(MultiplePermissionsReport report) {

                    List<PermissionGrantedResponse> list_GrantedPermission;

                    list_GrantedPermission = report.getGrantedPermissionResponses();

                    //check permission granted
                    if (report.areAllPermissionsGranted()){
                        intentList = addIntentsToList(mContext, intentList, takePhotoIntent);
                        intentList = addIntentsToList(mContext, intentList, pickIntent);

                    }

                    else if (list_GrantedPermission.size() > 0){
                        String grantedpermissionName = list_GrantedPermission.get(0).getPermissionName();

                        if (grantedpermissionName.equals(android.Manifest.permission.CAMERA))
                            intentList = addIntentsToList(mContext, intentList, takePhotoIntent);

                        else
                            intentList = addIntentsToList(mContext, intentList, pickIntent);
                    }
                    else {

                    }

                    //request a source
                    if (intentList.size() > 0) {
                        chooserIntent = Intent.createChooser(intentList.remove(intentList.size() - 1),
                                getString(R.string.message_select_source));
                        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.toArray(new Parcelable[]{}));

                        startActivityForResult(chooserIntent, REQUEST_CODE_PICTURE);
                    }

                    if (report.isAnyPermissionPermanentlyDenied()){
                        final AlertDialog.Builder alertBuilder = new AlertDialog.Builder(mContext);

                        alertBuilder.setTitle(getString(R.string.message_permission_required));
                        alertBuilder.setCancelable(true);

                        alertBuilder.setMessage(getString(R.string.message_permission_required));
                        alertBuilder.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.dismiss();
                            }
                        });
                        AlertDialog dialog = alertBuilder.create();
                        dialog.show();
                    }

                }

                @Override
                public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {
                    token.continuePermissionRequest();
                }

            }, android.Manifest.permission.CAMERA, android.Manifest.permission.READ_EXTERNAL_STORAGE);
        }
    });
4

1 回答 1

-1

我建议您查看有关拍摄或挑选照片的权限的答案

此外,ImagePicker是一种选择或拍照的简单方法。

于 2017-03-03T01:14:55.080 回答