简体   繁体   English

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

[英]Android how to request permission based on user action in chooser intent

I'm having a trouble to request user permission based on what they've selected in chooser intent. 我在根据用户在选择器意图中选择的内容来请求用户权限时遇到麻烦。 For now, my program is request the permissions before the chooser intent show up. 现在,我的程序需要在选择器意图显示之前请求权限。 How can I achieve in a way that request the permissions after they've selected intent. 如何在选择意图后以请求权限的方式实现。 The code below is request camera permission or storage permission first then only allow users to select they want to take picture from camera or gallery. 下面的代码是先请求相机权限或存储权限,然后才允许用户选择要从相机或图库中拍照的代码。

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

I suggest you take a look at this answer about permissions to take or pick pictures. 我建议你先看看这个回答有关权限采取或选择照片。

Also, ImagePicker is an easy way to pick or take a picture. 另外, ImagePicker是一种轻松拍摄或拍照的方法。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM