HelloWorld HelloWorld - 14 days ago 5
Android Question

How to force permission "android.permission.CAMERA" to be added to manifest in Codename one

I am trying to show a live preview of the rear facing camera in Codename One. I use for that the native interface (I am targetting Android first).

To allow my app to use the camera without messing up with build hints, I added these lines in my Main Form :

if (Capture.hasCamera()){
Dialog.show("Appareil photo détecté", "Votre matériel est bien équipé d'un appareil photo!", "OK", null);
}


I did that because Codename One blog stated


android.hardware.camera & android.permission.RECORD_AUDIO - are triggered by com.codename1.Capture


However while debugging the app with the sources I got from CN1 under Android Studio on a real device (API LEVEL 19 aka Kit Kat) I always got a
java.lang.RuntimeException: Fail to connect to camera service
.

After a while I realized that the exception was due to the lack of the following permission in manifest.xml :

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


Please note that as stated in CN1 blog the line
<uses-feature android:name="android.hardware.camera" android:required="false" />
was already added by CN1.


Now that I've added this line the live preview is well shown.

Why is this permission not automatically added in the manifest, should I really add it to the build hint ?

Moreover I have another project that uses the Camera to take photo and save them on the phone and the Camera permission is neither shown (in Android Application Info) nor requested during app installation whereas it works perfectly well. Please note that e.g. Twitter App shows "Take photos and record videos" in the app permissions so it does not seem to be a problem with my phone hiding camera permission.

EDIT November 20th 2016 :

Indeed Android Developer's guide states that


Note: If you are using the camera by invoking an existing camera app, your application does not need to request this permission.


That's why CN1 does not require this permission!

Thanks for helping me getting the permission management with CN1 right.

Answer

You need to add the following build hint:

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

You can also add this to the cn1lib so it is injected automatically.

Comments