How to force permission “android.permission.CAMERA

2019-02-10 16:06发布

问题:

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.

回答1:

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.



回答2:

In the past, you add the permission you want to your manifest and then it is requested when your user attempts to install your app. As of Android 6.0+, this has changed. Now you can just request a permission when your app needs it for the first time. After the user grants you permission, you don't need to ask again next time. Some benefits of this include keeping your user aware of why you need specific permissions and not requesting permissions that you don't actually need.

You can read more about how to implement it here: https://developer.android.com/training/permissions/requesting.html

There is no way to use a phones camera without asking for permission at some point.