【问题标题】:Crash when launch camera android permission [duplicate]启动相机android权限时崩溃[重复]
【发布时间】:2019-12-30 07:04:18
【问题描述】:

我正在使用导入的 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);
}

【问题讨论】:

  • 您必须请求 运行时权限 并在 API 24 及更高版本中获得用户的批准。在此处查看更多信息:developer.android.com/distribute/best-practices/develop/…
  • 只有在检查是否已经授予权限后才打开相机。如果不先请求许可。
  • @Rohit5k2 如何在相机启动时弹出权限?
  • 请查看我标记的帖子和@Md.Asaduzzaman 分享的链接

标签: java android android-camera android-permissions


【解决方案1】:

您可以在清单文件中将这些权限与其他权限一起使用,

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

【讨论】:

  • 如我所说,我已经授予这些权限
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-24
  • 2018-10-12
  • 1970-01-01
相关资源
最近更新 更多