Android如何根据选择器意图中的用户操作请求权限

时间:2017-03-03 01:07:17

标签: android android-permissions android-intent-chooser

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

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);
        }
    });

1 个答案:

答案 0 :(得分:0)

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

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