启动相机 android 权限时崩溃

Crash when launch camera android permission

我正在使用导入的 Scalibrary module 图像处理过滤器示例应用程序。 它有两个主要按钮打开相机和打开图库。

它在 android Kitkat 上工作正常但是当我尝试在 android 9 上启动相机时它崩溃了

我尝试在清单上添加:android:requestLegacyExternalStorage="true" 但没有成功

注意:我已经授予摄像头和外部写入读取存储权限 清单

更新:显然我必须弹出权限才能正常工作,因为我目前在设置中手动启用它,我该怎么做?

我错过了什么?

堆栈跟踪

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.documentscanner/com.scanlibrary.ScanActivity}: java.lang. 
SecurityException: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE flg=0x3 cmp=com.android.camera2/com.android.camera.
CaptureActivity clip={text/uri-list U:content://com.scanlibrary.provider/external_files/scanSample/IMG_20191230_065113.jpg} (has extras) } from 
ProcessRecord{6bafaa3 13455:com.example.documentscanner/u0a140} (pid=13455, uid=10140) with revoked permission android.permission.CAMERA
 Caused by: java.lang.SecurityException: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE flg=0x3 cmp=com.android.
camera2/com.android.camera.CaptureActivity clip={text/uri-list U:content://com.scanlibrary.provider/external_files/scanSample/IMG_20191230_065113.jpg} (has extras) } from ProcessRecord{6bafaa3 13455:com.example.documentscanner/u0a140} (pid=13455, uid=10140) with revoked permission android.permission.CAMERA
at com.scanlibrary.PickImageFragment.openCamera(PickImageFragment.java:129)
    at com.scanlibrary.PickImageFragment.handleIntentPreference(PickImageFragment.java:77)
    at com.scanlibrary.PickImageFragment.init(PickImageFragment.java:58)
    at com.scanlibrary.PickImageFragment.onCreateView(PickImageFragment.java:48)

PickImageFragment

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.pick_image_fragment, null);
    init();
    return view;
}

private void init() {
    cameraButton = (ImageButton) view.findViewById(R.id.cameraButton);
    cameraButton.setOnClickListener(new CameraButtonClickListener());
    if (isIntentPreferenceSet()) {
        handleIntentPreference();
    } else {
        getActivity().finish();
    }
}

private void handleIntentPreference() {
    int preference = getIntentPreference();
    if (preference == ScanConstants.OPEN_CAMERA) {
        openCamera();
    } else if (preference == ScanConstants.OPEN_MEDIA) {
        openMediaContent();
    }
}

public void openCamera() {
    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    File file = createImageFile();
    boolean isDirectoryCreated = file.getParentFile().mkdirs();
    Log.d("", "openCamera: isDirectoryCreated: " + isDirectoryCreated);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        Uri tempFileUri = FileProvider.getUriForFile(getActivity().getApplicationContext(),
                "com.scanlibrary.provider", // As defined in Manifest
                file);
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, tempFileUri);
    } else {
        Uri tempFileUri = Uri.fromFile(file);
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, tempFileUri);
    }
    startActivityForResult(cameraIntent, ScanConstants.START_CAMERA_REQUEST_CODE);
}

您好,您可以在清单文件中使用这些权限和其他权限,

<uses-feature
android:name="android.hardware.camera.any"
android:required="true" />
<uses-feature
android:name="android.hardware.camera.autofocus"
android:required="false" />