1. 程式人生 > >呼叫系統相機拍照出現java.lang.SecurityException:

呼叫系統相機拍照出現java.lang.SecurityException:

具體問題:
java.lang.SecurityException: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE flg=0x3 cmp=com.android.camera/.Camera clip={text/uri-list U:content://media/external/images/media/59625} (has extras) } from ProcessRecord{51c6155 13371:com.powervision.gcs/u0a508} (pid=13371, uid=10508) with revoked permission android.permission.CAMERA
這個安全異常是starting Intent時引起的,具體程式碼:

Intent takeIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

最後面的提示是with revoked permission android.permission.CAMERA,因為Camera許可權是一個危險許可權,這兒在呼叫相機時需要去動態判斷這個許可權(sdk>22),增加許可權驗證後問題解決,一下為程式碼:

    /**
     * 檢查拍照許可權
     */
    private void checkPermission() {
        if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            // 進入這兒表示沒有許可權
if (ActivityCompat.shouldShowRequestPermissionRationale(mActivity, Manifest.permission.CAMERA)) { // 提示已經禁止 ToastUtil.longToast(mContext, getString(R.string.you_have_cut_down_the_permission)); } else { ActivityCompat.requestPermissions(mActivity, new
String[]{Manifest.permission.CAMERA}, 100); } } else { takePhotoByCamera(); } }