当我尝试在Android 7.0中打开相机时应用程序崩溃

时间:2017-06-19 10:20:23

标签: java android

在我的应用程序中,有一个使用Camera捕获图像的工具。 它适用于所有版本,直到Android 6,但它使我的应用程序在Android 7.0版本中崩溃。

清单

<uses-permission android:name="android.permission.CAMERA" />

已添加权限

if (ContextCompat.checkSelfPermission(AddFamilyMemberActivity.this,
                android.Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(AddFamilyMemberActivity.this,
                    new String[]{android.Manifest.permission.CAMERA},
                    1);
        } else {
            sendToExternalApp();
        }

权限监听器

@Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == 1 && permissions[0].equals(android.Manifest.permission.CAMERA) &&
                grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            sendToExternalApp();
        } else if (requestCode == 1 && permissions[0].equals(android.Manifest.permission.CAMERA) &&
                grantResults[0] == PackageManager.PERMISSION_DENIED) {
            Utilities.showSnackBar("You need to enable camera permission for update profile image", this);
        }
    }

打开相机的代码

protected void sendToExternalApp()
    {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        mProcessingPhotoUri =  getImageFile();
        intent.putExtra(MediaStore.EXTRA_OUTPUT, mProcessingPhotoUri);
        activity.startActivityForResult(intent, REQUEST_CODE_SELECT_IMAGE);
    }

崩溃日志

引起:     android.content.ActivityNotFoundException:找不到处理Intent的Activity {act = android.media.action.IMAGE_CAPTURE flg = 0x3 clip = {text / uri-list U:file:/// storage / emulated / 0 / DCIM / QDhealth / profile.jpg}(有额外内容)}

1 个答案:

答案 0 :(得分:0)

您的第一个屏幕检查下面给出的许可

onCreate(或)初始化

if ((ContextCompat.checkSelfPermission(LoginActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) {
    requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA},
                    MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
}

覆盖以下方法

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode) {
        case PERMISSIONS_CODE:
            if (grantResults.length <= 0 || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show();
            }
            break;
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            break;
    }
}