【问题标题】:My simple camera app (android) won't load the camera view after the first attempt我的简单相机应用程序(android)在第一次尝试后不会加载相机视图
【发布时间】:2017-03-24 09:18:30
【问题描述】:

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

这是 MainActivity 代码:

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() {

    }


}

【问题讨论】:

    标签: android camera android-camera runtime-permissions


    【解决方案1】:

    我相信你缺少“else”

    if (ContextCompat.checkSelfPermission(this,
                    Manifest.permission.CAMERA)
                    != PackageManager.PERMISSION_GRANTED) {
    // your code
    
    } else {
       // stuff with camera
       try{
          mCamera = Camera.open(1);
       } catch (Exception e){
       ...
    }
    

    【讨论】:

    • 我很困惑我缺少“else”的地方......我的“try”块使用 onRequestPermissionsResult 方法......我应该将它移到 onCreate 方法吗? (在“if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)”区域内)
    • 您应该将其复制到 onCreate 方法。 @Vlad Matvienko 解释道。
    【解决方案2】:

    你只在onActivityResult启动摄像人员,只有在你没有权限时才会调用,并请求他们:

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

    这个if 需要else 用于权限已经是PERMISSION_GRANTED 的情况。你必须在这里做和onActivityResult一样的工作人员:

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

    【讨论】:

    • 我已将 try 块复制到 else 语句(就在 != 到 PERMISSION_GRANTED 之外),但我仍然得到相同的结果...我想我误解了你的答案跨度>
    • @fmi,你有什么例外吗?
    • 不....第一次加载应用程序时,它会提示我请求....然后前置摄像头顶部显示 2 个图标....关闭应用程序后并重新打开,我不再收到提示,我只看到一个白色屏幕,顶部有 2 个图标,不再对触摸做出反应
    • 你能更新你问题中的代码让我看看也许我找到了一些东西。
    • 我刚刚更新了代码,但我注意到我收到了一个警告......我的“addView”调用“可能会产生空指针异常”......它在我的 swapCamera() 方法中
    猜你喜欢
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-06
    • 1970-01-01
    • 2013-09-17
    • 1970-01-01
    相关资源
    最近更新 更多