Android-已撤销权限android.permission.CAMERA

时间:2019-01-14 11:01:27

标签: android kotlin android-camera-intent

我像下面这样用kotlin打开相机:

private fun dispatchTakePictureIntent() {
    Intent(MediaStore.ACTION_IMAGE_CAPTURE).also { takePictureIntent ->
        // Create the File where the photo should go
        val photoFile: File? = try {
            activity?.createImageFile()
        } catch (ex: IOException) {
            Timber.e(ex)
            null
        }
        // Continue only if the File was successfully created
        photoFile?.also {
            takenPhotoUri = FileProvider.getUriForFile(
                    requireContext(), "zz.zz.android.fileprovider", it
            )
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, takenPhotoUri)
            startActivityForResult(takePictureIntent, REQUEST_CODE_FOR_PERFORM_CAMERA)
        }
    }
}

但是让我听到以下错误:

java.lang.SecurityException: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE flg=0x3 cmp=com.sec.android.app.camera/.Camera launchParam=MultiScreenLaunchParams { mDisplayId=0 mFlags=0 } clip={text/uri-list U:content://zz.zz.android.fileprovider/zz_zz_chat_images/PNG_%DB%B2%DB%B0%DB%B1%DB%B9%DB%B0%DB%B1%DB%B1%DB%B4_%DB%B1%DB%B4%DB%B2%DB%B2%DB%B5%DB%B5_1904526976.png} (has extras) } from ProcessRecord{e956b7f 13929:zz.zz.android.dev/u0a1605} (pid=13929, uid=11605) with revoked permission android.permission.CAMERA

我正在使用intent中的相机。

这是我的onActivityResult

    override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
        if (resultCode == Activity.RESULT_OK) {
            when (requestCode) {
                REQUEST_CODE_FOR_PERFORM_CAMERA -> takenPhotoUri?.let { uri ->
                    uri.getBitmpap(requireContext())?.let { bitmap ->
                        presenter.uploadImage(chatRoomId, "image/png", uri, bitmap, "")
                    }
                }
}}

1 个答案:

答案 0 :(得分:0)

在清单中使用相机权限

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

并且需要棉花糖运行时间以上的时间权限

喜欢

 if (ContextCompat.checkSelfPermission(getContext(),
            Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {

        if (ActivityCompat.shouldShowRequestPermissionRationale((Activity)
                getContext(), Manifest.permission.CAMERA)) {


        } else {
            ActivityCompat.requestPermissions((Activity) getContext(), 
                    new String[]{Manifest.permission.CAMERA},
                    MY_PERMISSIONS_REQUEST_CAMERA);
        }

    }

在onRequestPermissionsResult检查是否授予权限

 @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case PERMISSION_REQUEST_CODE:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(getApplicationContext(), "Permission Granted", Toast.LENGTH_SHORT).show();

                    // main logic
                } else {
                    Toast.makeText(getApplicationContext(), "Permission Denied", Toast.LENGTH_SHORT).show();}
}