简体   繁体   English

如何为片段中的Camera授予权限

[英]How to grant permissions for the Camera in Fragment

Hello I had simple application where I am using the camera, so the user must grant permission for the camera. 您好,我使用相机的应用程序很简单,因此用户必须授予相机权限。

So far my app had only Activity , but now I want to use Fragments . 到目前为止我的应用程序只有Activity ,但现在我想使用Fragments I got to fragments one is called FragmentScanCode (here I wanna use the camera ). 我得到片段一个叫做FragmentScanCode (这里我想用相机)。

This is also the first fragment so I want to grant permission at the start of app. 这也是第一个片段,所以我想在app开始时授予权限。

There is my code from MainActivity , it works fine. 有来自MainActivity代码,它运行正常。

final int RequestCameraPermissionID = 1001;

onRequestPermissionsResult : onRequestPermissionsResult

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case RequestCameraPermissionID: {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                        ActivityCompat.requestPermissions(MainActivity.this,
                                new String[]{Manifest.permission.CAMERA},
                                RequestCameraPermissionID);

                        //return;
                    }
                    try {
                        cameraSource.start(cameraView.getHolder());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
            }
            break;
        }
    }

I can't use this in my Fragment because ActivityCompat . 我不能在我的Fragment使用它,因为ActivityCompat

Is any possible solution to grant permission on fragment? 是否有任何可能的解决方案来授予片段权限?

This is what I tried based on developer.google.com doc and it works 这是我基于developer.google.com doc尝试的内容,它可以正常运行

This is working code for me. 这是我的工作代码。

@Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                           int[] grantResults) {
        switch (requestCode){
            case RequestCameraPermissionID: {
                if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    if(getActivity().checkSelfPermission(Manifest.permission.CAMERA) !=
                    PackageManager.PERMISSION_GRANTED) {
                        requestPermissions(
                                new String[]{Manifest.permission.CAMERA},
                                RequestCameraPermissionID);
                        //cameraSource.start(cameraView.getHolder());
                        return;
                    }
                }
                captureCode();
            }
            break;
        }
    }

and also in my function captureCode() 还有我的函数captureCode()

try {
                        if (getActivity().checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {

                            requestPermissions(
                                    new String[]{Manifest.permission.CAMERA},
                                    RequestCameraPermissionID);
                            return;
                        }
                        cameraSource.start(cameraView.getHolder());

                    } catch (IOException e) {
                        e.printStackTrace();
                    }

This behavior seems to be present in the v4 Fragment support class requestPermissions in Fragment. 这种行为似乎存在于Fragment中的v4 Fragment支持类requestPermissions中。 The Activity/FragmentCompat implementations exist for people who wish to use the native classes with the extended functionality on API levels between 11 and 23. check this out 活动/ FragmentCompat实现对谁希望与11和23之间的API级别的扩展功能使用本地类人存在检查了这一点

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

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