Camera permission below API 23 only

1.5k views Asked by At

I've made an simple flash application which toggles the flashlight. Everything works fine, but I'm having a few issues with the permission on API higher than 23.

In API 23 a new method was added for handling the flashlight mode. More precisely CameraManager.setTorchMode(String cameraId, boolean enabled). This new method does not require to open the camera anymore just for handling the flashlight actions.

My problem is that below API 23, this method does not exist thus I need to access the camera, this forcing me to use the camera permission: <uses-permission android:name="android.permission.CAMERA"/> in Manifest.

But I want to avoid requesting the camera permission on API higher than 23 as this won't affect the functionality of the application.

Does anyone know how I can request the camera permission only on APIs below 23?

2

There are 2 answers

1
Ionut Negru On BEST ANSWER

I found the solution for this. It seems the uses-permission allows for some customization and I found android:maxSdkVersion property to do just what I want.

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

I hope this helps others in a similar situation.

More on this can be found in the official documentation: uses-permission

0
Vova On

Try this permission in the manifest file:
<uses-feature android:name="android.hardware.camera2"/> <uses-feature android:name="android.hardware.camera" />