Nativescript: The Camera module in Android doesn't handle pemissions for you

Created on 28 Sep 2016  路  8Comments  路  Source: NativeScript/NativeScript

Please, provide the details below:

Did you verify this is a real problem by searching [Stack Overflow]

Yep!

Tell us about the problem

Using the Camera plugin on Android N requires permissions updates. Currently this requires using another plugin, nativescript-permissions, but this should be baked into the Camera code itself.

Which platform(s) does your issue occur on?

Android

Please provide the following version numbers that your issue occurs with:

  • CLI: 2.3.0
  • Cross-platform modules: 2.3.0
  • Runtime(s): 2.3.0
  • Plugin(s):

    Please tell us how to recreate the issue in as much detail as possible.

Try to use the Camera on Android (most recent version).

bug done android

Most helpful comment

From what I skimmed through the camera module in the core-modules isn't handling any runtime permissions which is going to cause headaches for onboarding devs with no native android experience, even more so those coming from a pre-lollipop android world. IMO - moving the camera into an external plugin from the NS team would be ideal so the manifest doesn't add the Camera permission by default unless the camera plugin is installed by the developer.

All 8 comments

From what I skimmed through the camera module in the core-modules isn't handling any runtime permissions which is going to cause headaches for onboarding devs with no native android experience, even more so those coming from a pre-lollipop android world. IMO - moving the camera into an external plugin from the NS team would be ideal so the manifest doesn't add the Camera permission by default unless the camera plugin is installed by the developer.

I can confirm that the camera module is not working in Android 7.
At my side, the error stack returns the following when trying to take a picture.

JS: Error: android.os.FileUriExposedException: file:///storage/emulated/0/Android/data/org.nativescript.cameraModuleTest2/files/cameraPicture_2909201610115.jpg exposed beyond app through
 ClipData.Item.getUri()                                                                                                                                                                   
JS:     android.os.StrictMode.onFileUriExposed(StrictMode.java:1799)                                                                                                                      
JS:     android.net.Uri.checkFileUriExposed(Uri.java:2346)                                                                                                                                
JS:     android.content.ClipData.prepareToLeaveProcess(ClipData.java:832)                                                                                                                 
JS:     android.content.Intent.prepareToLeaveProcess(Intent.java:8909)                                                                                                                    
JS:     android.content.Intent.prepareToLeaveProcess(Intent.java:8894)                                                                                                                    
JS:     android.app.Instrumentation.execStartActivity(Instrumentation.java:1517)                                                                                                          
JS:     android.app.Activity.startActivityForResult(Activity.java:4224)                                                                                                                   
JS:     android.app.Activity.startActivityForResult(Activity.java:4183)                                                                                                                   
JS:     com.tns.Runtime.callJSMethodNative(Native Method)                                                                                                                                 
JS:     com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:865)                                                                                                                      
JS:     com.tns.Runtime.callJSMethodImpl(Runtime.java:730)                                                                                                                                
JS:     com.tns.Runtime.callJSMethod(Runtime.java:716)                                                                                                                                    
JS:     com.tns.Runtime.callJSMethod(Runtime.java:697)                                                                                                                                    
JS:     com.tns.Runtime.callJSMethod(Runtime.java:687)                                                                                                                                    
JS:     com.tns.gen.android.view.View_OnClickListener.onClick(android.view.View$OnClickListener.java)                                                                                     
JS:     android.view.View.performClick(View.java:5609)                                                                                                                                    
JS:     android.view.View$PerformClick.run(View.java:22259)                                                                                                                               
JS:     android.os.Handler.handleCallback(Handler.java:751)                                                                                                                               
JS:     android.os.Handler.dispatchMessage(Handler.java:95)                                                                                                                               
JS:     android.os.Looper.loop(Looper.java:154)                                                                                                                                           
JS:     android.app.ActivityThread.main(ActivityThread.java:6077)                                                                                                                         
JS:     java.lang.reflect.Method.invoke(Native Method)                                                                                                                                    
JS:     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)                                                                                                   
JS:     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)                                                                                                                      
JS:     at Error (native)                                                                                                                                                                 
JS:     at /data/data/org.nativescript.cameraModuleTest2/files/app/tns_modules/camera/camera.js:80:56                                                                                     
JS:     at Object.exports.takePicture (/data/data/org.nativescript.cameraModuleTest2/files/app/tns_modules/camera/camera.js:4:12)                                                         
JS:     at Object.onTap [as callback] (/data/data/org.nativescript.cameraModuleTest2/files/app/main-page.js:19:14)                                                                        
JS:     at Button.Observable.notify (/data/data/org.nativescript.cameraModuleTest2/files/app/tns_modules/data/observable/observable.js:149:23)                                            
JS:     at Button.Observable._emit (/data/data/org.nativescript.cameraModuleTest2/files/app/tns_modules/data/observable/observable.js:168:18)                                             
JS:     at _android.setOnClickListener.android.view.View.OnClickListener.onClick (/data/data/org.nativescript.cameraModuleTest2/files/app/tns_modules/ui/button/button.js:33:32)  

Even after explicitly granted permissions with the code below the error is still thrown.

    permissions.requestPermission(["android.permission.WRITE_EXTERNAL_STORAGE", "android.permission.CAMERA"], "I need these permissions because I'm cool")
    .then(function() {
        console.log("Woo Hoo, I have the power!");
    })
    .catch(function() {
        console.log("Uh oh, no permissions - plan B time!");
    });

Probably related to https://github.com/NativeScript/NativeScript/blob/master/tns-core-modules/camera/camera.android.ts#L39 and this thread

That's the error I got as well. If you edit your Android manifest to target 23 it seems to correct it.

Yes, it is related to the permission changes in 7. We are now moving the camera module in a separate plugin and will release it _next week_ in order to provide a clean solution for the problem.

Thanks for the heads up!

Hi all,

We've just finished (initial) work on native plugin for using device camera hardware. We have some issues to publish it to npm (there is a plugin with the same name :-(). We will try to get it - in case we succeed we will publish it like nativescript-camera. In worst case scenario we should change the name of the plugin which will not affect working code just some references. However you could use the current version of the plugin from its github repo. Within this repo there is an example how to use it (be aware that you will need latest tns-core-modules and tns-core-modules-widgets a.k.a. next. In order to produce package just run create.sh script from plugin root folder.

Let me know if you need some other assistance.

Nedyalko

We added a special method to nativescript-camera plugin called requestPermissions() which should be called to request camera and write to external storage permissions (required to be able to share image on Google Photos). NativeScript-Camera plugin adds necessary uses-permission clauses on app prepare stage.

P.S. I'll close the issue, feel free to reopen it if needed.

I am getting 'can't connect to camera' error. I have nativescript-camera and nativescript-permissions plugin and granted permissions. But don't know why this getting error.

Any help??I need urgently

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings