Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的简单相机应用程序(android)赢得了';t在第一次尝试后加载相机视图_Android_Camera_Android Camera_Runtime Permissions - Fatal编程技术网

我的简单相机应用程序(android)赢得了';t在第一次尝试后加载相机视图

我的简单相机应用程序(android)赢得了';t在第一次尝试后加载相机视图,android,camera,android-camera,runtime-permissions,Android,Camera,Android Camera,Runtime Permissions,我只是想制作一个使用运行时权限的简单相机应用程序……在第一次启动应用程序(请求相机权限)时,它似乎可以正常加载。在允许访问后,它工作了…但一旦我关闭它并再次启动它,它就会显示一个白色图像,带有我的图标,不会响应。我已经在应用程序中手动检查了权限,相机仍然被授予访问权限,但我想我把权限代码搞乱了 以下是主要活动代码: public class MainActivity extends AppCompatActivity { private static final int MY_PERM

我只是想制作一个使用运行时权限的简单相机应用程序……在第一次启动应用程序(请求相机权限)时,它似乎可以正常加载。在允许访问后,它工作了…但一旦我关闭它并再次启动它,它就会显示一个白色图像,带有我的图标,不会响应。我已经在应用程序中手动检查了权限,相机仍然被授予访问权限,但我想我把权限代码搞乱了

以下是主要活动代码:

public class MainActivity extends AppCompatActivity {

    private static final int MY_PERMISSIONS_REQUEST_CAMERA = 1;
    private Camera mCamera = null;
    private Camera mCameraFront = null;
    private CameraView mCameraView = null;
    public int switchCamera = 1;


//    int permissionCheck = ContextCompat.checkSelfPermission(this,
//            Manifest.permission.CAMERA);

//    String[] perms = {"android.permission.CAMERA"};


//    int permsRequestCode = 200;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED) {

            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.CAMERA)) {

                // Show an explanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.

            } else {

                // No explanation needed, we can request the permission.

                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.CAMERA},
                        MY_PERMISSIONS_REQUEST_CAMERA);

                try {
                    mCamera = Camera.open(1);//you can use open(int) to use different cameras
                } catch (Exception e) {
                    Log.d("ERROR", "Failed to get camera: " + e.getMessage());
                }
                SwapCamera();

//                if (mCamera != null) {
////                        mCameraView = new CameraView(this, mCamera);//create a SurfaceView to show camera data
////                        FrameLayout camera_view = (FrameLayout)findViewById(R.id.camera_view);
////                        camera_view.addView(mCameraView);//add the SurfaceView to the layout
//                    SwapCamera();
//                }

                //btn to close the application
                ImageButton imgClose = (ImageButton) findViewById(R.id.imgClose);
                imgClose.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mCamera.setPreviewCallback(null);
                        mCamera.setErrorCallback(null);
                        mCamera.stopPreview();
                        mCamera.release();
                        mCamera = null;
                        System.exit(0);
                    }
                });

//                     btn to switch camera
                ImageButton imgSwitch = (ImageButton) findViewById(R.id.cameraSwitch);
                imgSwitch.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
//                            switchCamera++;
                    }
                });


            }
        }
    }



    public void SwapCamera() {
        mCameraView = new CameraView(this, mCamera);//create a SurfaceView to show camera data
        FrameLayout camera_view = (FrameLayout)findViewById(R.id.camera_view);
        camera_view.addView(mCameraView);//add the SurfaceView to the layout
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {

        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_CAMERA: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {



                    // permission was granted, yay! Do the
                    // camera-related task you need to do.

                    try{
                        mCamera = Camera.open(1);//you can use open(int) to use different cameras
                    } catch (Exception e){
                        Log.d("ERROR", "Failed to get camera: " + e.getMessage());
                    }

                    if(mCamera != null) {
//                        mCameraView = new CameraView(this, mCamera);//create a SurfaceView to show camera data
//                        FrameLayout camera_view = (FrameLayout)findViewById(R.id.camera_view);
//                        camera_view.addView(mCameraView);//add the SurfaceView to the layout
                        SwapCamera();
                    }

                    //btn to close the application
                    ImageButton imgClose = (ImageButton)findViewById(R.id.imgClose);
                    imgClose.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            mCamera.setPreviewCallback(null);
                            mCamera.setErrorCallback(null);
                            mCamera.stopPreview();
                            mCamera.release();
                            mCamera = null;
                            System.exit(0);
                        }
                    });

//                     btn to switch camera
                    ImageButton imgSwitch = (ImageButton)findViewById(R.id.cameraSwitch);
                    imgSwitch.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
//                            switchCamera++;
                       }
                    });



                } else {

                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                }
                return;
            }

            // other 'case' lines to check for other
            // permissions this app might request
        }
    }

    public void onActivityResult() {

    }


}
我相信你错过了“其他”

我相信你错过了“其他”


您只能在ActivityResult的
中启动camera staff,只有在您没有权限时才会调用它,并请求它们:

if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED) {
    ...
}
对于已授予权限的情况,此
if
需要
else
。您必须在这里执行与在活动结果中执行的
相同的操作:

else {
    try{
                            mCamera = Camera.open(1);//you can use open(int) to use different cameras
                        } catch (Exception e){
                            Log.d("ERROR", "Failed to get camera: " + e.getMessage());
                        }

                        if(mCamera != null) {
    //                        mCameraView = new CameraView(this, mCamera);//create a SurfaceView to show camera data
    //                        FrameLayout camera_view = (FrameLayout)findViewById(R.id.camera_view);
    //                        camera_view.addView(mCameraView);//add the SurfaceView to the layout
                            SwapCamera();
                        }

                        //btn to close the application
                        ImageButton imgClose = (ImageButton)findViewById(R.id.imgClose);
                        imgClose.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                mCamera = null;
                                System.exit(0);
                            }
                        });

    //                     btn to switch camera
                        ImageButton imgSwitch = (ImageButton)findViewById(R.id.cameraSwitch);
                        imgSwitch.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
    //                            switchCamera++;
                           }
                        });
}

您只能在ActivityResult的
中启动camera staff,只有在您没有权限时才会调用它,并请求它们:

if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED) {
    ...
}
对于已授予权限的情况,此
if
需要
else
。您必须在这里执行与在活动结果中执行的
相同的操作:

else {
    try{
                            mCamera = Camera.open(1);//you can use open(int) to use different cameras
                        } catch (Exception e){
                            Log.d("ERROR", "Failed to get camera: " + e.getMessage());
                        }

                        if(mCamera != null) {
    //                        mCameraView = new CameraView(this, mCamera);//create a SurfaceView to show camera data
    //                        FrameLayout camera_view = (FrameLayout)findViewById(R.id.camera_view);
    //                        camera_view.addView(mCameraView);//add the SurfaceView to the layout
                            SwapCamera();
                        }

                        //btn to close the application
                        ImageButton imgClose = (ImageButton)findViewById(R.id.imgClose);
                        imgClose.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                mCamera = null;
                                System.exit(0);
                            }
                        });

    //                     btn to switch camera
                        ImageButton imgSwitch = (ImageButton)findViewById(R.id.cameraSwitch);
                        imgSwitch.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
    //                            switchCamera++;
                           }
                        });
}

我不知道我在哪里缺少了“else”…我有onRequestPermissionsResult方法的“try”块…我应该把它移到onCreate方法吗?(在“if(ContextCompat.checkSelfPermission(this,Manifest.permission.CAMERA)!=PackageManager.permission_grated)”区域内)应将其复制到onCreate方法@Vlad Matvienko解释道。我很困惑我在哪里遗漏了“else”…我有onRequestPermissionsResult方法的“try”块…我应该将它移到onCreate方法吗?(在“if(ContextCompat.checkSelfPermission(this,Manifest.permission.CAMERA)!=PackageManager.permission_grated)”区域内)应将其复制到onCreate方法@Vlad Matvienko解释说,我已经将try块复制到else语句中(仅超出了!=授予的权限),但我仍然得到相同的结果…我想我误解了你的意思answer@fmi,你有什么例外吗?没有…我第一次加载应用时,它会提示我请求…前向摄像头会显示两个图标,直到…关闭应用程序并重新打开后,我不再得到提示,我只看到一个白色屏幕,上面有两个图标,对触摸不再有反应。你可以更新你问题中的代码让我看看,也许我发现了什么。我刚刚更新了代码,但我注意到我收到了一条警告…我的“addView”调用“可能会产生空指针异常”…这是在我的swapCamera()方法中。我已将try块复制到else语句中(仅超出授予的!=权限),但仍然得到相同的结果…我想我误解了您的answer@fmi,你有什么例外吗?没有…我第一次加载应用时,它会提示我请求…前向摄像头会显示两个图标,直到…关闭应用程序并重新打开后,我不再得到提示,我只看到一个白色屏幕,上面有两个图标,对触摸不再有反应。你可以更新你问题中的代码让我看看,也许我发现了什么。我刚刚更新了代码,但我注意到我收到了一条警告…我的“addView”调用“可能会产生空指针异常”…它在我的swapCamera()方法中