Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
347 views
in Technique[技术] by (71.8m points)

android - How to solve java.lang.SecurityException while trying to take a photo using camera in Kotlin?

I am trying to take a photo in kotlin with the following function:

@JavascriptInterface
fun generate() {
    val photoUri = Uri.fromFile(photo)
    val imageCaptureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
        .putExtra(MediaStore.EXTRA_OUTPUT, photoUri)
    startActivityForResult(imageCaptureIntent, GO_TO_CARD_REQUEST_CODE)
}

My manifest has following permissions and features granted:

<uses-feature android:name="android.hardware.camera.any" android:required="true" />
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />

When starting the activity an exception is shown in Logcat: "java.lang.SecurityException: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE flg=0x3 cmp=com.android.camera/.Camera". The camera application does not even start. How to solve this error? Thanks for any help.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The CAMERA permission is if you are using the camera directly in your app, via the Android SDK's camera API or libraries around them. ACTION_IMAGE_CAPTURE launches an external camera app — that app uses the API and needs the permission. So, the simplest solution is to remove the CAMERA permission.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...