仅在单击按钮后请求相机权限

request camera permission only after button is clicked

我创建了一个应用程序,我可以在其中放置个人资料图片,它看起来如下:

点击相机按钮时有两种选择:

现在,我想让我的应用在用户单击“相机”选项后询问他们是否允许我授予相机权限,但我不知道该怎么做。

目前,应用程序会在用户第一次输入此内容时请求权限 activity。

如何让授予权限仅在单击此 Cemra 按钮后弹出?

我的代码是:

private void showPictureDialog(){
    AlertDialog.Builder pictureDialog = new AlertDialog.Builder(this);
    pictureDialog.setTitle("Select Action");
    String[] pictureDialogItems = {"Photo Gallery", "Camera" };
    pictureDialog.setItems(pictureDialogItems,
            (dialog, which) -> {
                switch (which) {
                    case 0:
                        choosePhotoFromGallary();
                        break;
                    case 1:
                        takePhotoFromCamera();
                        break;
                }
            } );
    pictureDialog.show();
}
public void choosePhotoFromGallary() {
    Intent galleryIntent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

    startActivityForResult(galleryIntent, AppConstants.GALLERY);
}

private void takePhotoFromCamera() {
    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, AppConstants.CAMERA);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_CANCELED) {
        return;
    }
    Bitmap fixBitmap;
    if (requestCode == AppConstants.GALLERY) {
        if (data != null) {
            Uri contentURI = data.getData();
            try {
                DOES SOMETHING
            } catch (IOException ignored) {

            }
        }

    } else if (requestCode == AppConstants.CAMERA) {
            DOES SOMETHING
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == 5) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

        }

    }
}

谢谢

为了请求权限,我使用了来自 Karumi 的很棒的库 Dexter:

https://github.com/Karumi/Dexter

我和你有同样的情况,我的做法很简单:

txt_choose_from_gallery.setOnClickListener {
    checkPhotoPermissions {
        choosePictureFromGallery()
    }
}

txt_take_picture.setOnClickListener {
    checkPhotoPermissions {
        takePictureWithCamera()
    }
}

private fun checkPhotoPermissions(onPermissionGranted: () -> Unit) {
        Dexter.withActivity(activity)
            .withPermissions(
                android.Manifest.permission.READ_EXTERNAL_STORAGE,
                android.Manifest.permission.WRITE_EXTERNAL_STORAGE
            )
            .withListener(object : BaseMultiplePermissionsListener() {
                override fun onPermissionsChecked(response: MultiplePermissionsReport) {
                    if (response.areAllPermissionsGranted()) {
                        onPermissionGranted()
                    }
                }
            })
            .check()
    }

你可以很容易的把它转换成Java,只需要将高阶函数onPermissionGranted: () -> Unit改成接口,例如:

interface OnPermissionGrantedListner {
    void onPermissionGranted();
}

private void checkPhotoPermission(OnPermissionGrantedListener listener) {
    // Dexter code for checking permission, invoke listener in overridden onPermissionChecked method in BaseMultiplePermissionsListener
    listener.onPermissionGranted()
}

private void showPictureDialog(){
    AlertDialog.Builder pictureDialog = new AlertDialog.Builder(this);
    pictureDialog.setTitle("Select Action");
    String[] pictureDialogItems = {"Photo Gallery", "Camera" };
    pictureDialog.setItems(pictureDialogItems,
            (dialog, which) -> {
                switch (which) {
                    case 0:
                        checkPhotoPermission(new OnPermissionGrantedListener {
                            @Override
                            void onPermissionGranted() {
                                choosePhotoFromGallery();
                            }
                        }
                        break;
                    case 1:
                        checkPhotoPermission(new OnPermissionGrantedListener {
                            @Override
                            void onPermissionGranted() {
                                takePhotoFromCamera();
                            }
                        }                            
                        break;
                }
            } );
    pictureDialog.show();
}

您的问题的想法是:您应该在用户单击对话框中的“相机”选项时请求相机许可,而不是在创建 activity 时请求相机许可

下面是我的解决方案:

首先删除您当前的“请求相机权限”代码

private void showPictureDialog(){
    ...
                    case 1:
                        checkPermissionAndOpenCamera();
                        break;
    ...
}

/**
 * This will check your app camera permission.
 * If its granted, open camera
 * else request camera permission then open it later.
 */
void checkPermissionAndOpenCamera() {
    if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA)
            == PackageManager.PERMISSION_DENIED) {
        ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.CAMERA}, 5);
    } else {
        takePhotoFromCamera();
    }
}


private void takePhotoFromCamera() {
    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, AppConstants.CAMERA);
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == 5) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            takePhotoFromCamera();
        }

    }
}