【问题标题】:Open dialog doesn't open the camera app打开对话框不会打开相机应用程序
【发布时间】:2017-06-10 21:51:22
【问题描述】:

我的应用中有一个按钮,它必须是打开相机,这是我的代码:

    mPhotoButton = (ImageButton) v.findViewById(R.id.crime_camera);
    final Intent captureImage = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    boolean canTakePhoto = mPhotoFile != null &&
            captureImage.resolveActivity(packageManager) != null;
    mPhotoButton.setEnabled(canTakePhoto);

    if (canTakePhoto) {
        Uri uri = Uri.fromFile(mPhotoFile);
        captureImage.putExtra(MediaStore.EXTRA_OUTPUT, uri);
    }

    mPhotoButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivityForResult(captureImage, REQUEST_PHOTO);
        }
    });

当我按下它时,应用程序崩溃了。

如果使用createChooser(),在运行应用程序并按下按钮时,打开选择相机应用程序的对话框,它不会打开相机应用程序并且应用程序崩溃。

我在清单中定义了相机的权限。

此代码运行在另一台设备上,但在我的设备上运行时 (Galaxy S7 API24) 发生错误。

【问题讨论】:

    标签: android android-intent camera


    【解决方案1】:

    这似乎是一个权限问题。 如果您在 api 级别 23 或更高级别上运行您的应用,则必须在运行时请求许可。

    if (ContextCompat.checkSelfPermission(thisActivity,
                    Manifest.permission.CAMERA)
            != PackageManager.PERMISSION_GRANTED) {
    
        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
                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(thisActivity,
                    new String[]{Manifest.permission.CAMERA},
                    MY_PERMISSIONS_REQUEST_CAMERA);
    
            // MY_PERMISSIONS_REQUEST_CAMERA is an
            // app-defined int constant. The callback method gets the
            // result of the request.
        }
    }
    

    然后处理结果

    @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 task you need to do.
    
                } 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
        }
    }
    

    欲了解更多信息,请访问Android Runtime Permissions

    【讨论】:

    • MY_PERMISSIONS_REQUEST_CAMERA 是什么?
    • MY_PERMISSIONS_REQUEST_CAMERA 是应用定义的 int 常量。使用此常量,您可以在 onRequestPermissionsResult 中识别所请求的权限。示例 int MY_PERMISSIONS_REQUEST_CAMERA = 1;
    • 我添加了你说的代码,当我按下按钮时,打开对话框以获得许可,但应用程序再次崩溃
    • ifelse 中的语句在 onRequestPermissionsResult() 方法?
    • 我将 targetSdkVersion 从 25 更改为 21 并解决了问题,,,,谢谢先生
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-20
    • 1970-01-01
    • 1970-01-01
    • 2019-08-20
    相关资源
    最近更新 更多