Android Question QRCodeReaderView - problem in a device without camera

mmanso

Active Member
Licensed User
Longtime User
Hi all,

I'm using QRCodeReaderView in a project that wheen we activate a certain config flag, we can read QRcodes.

I've a QRCodeReaderView in my layout that I use when that flag is active.

The app works great in devices that has camera. When I install the app in a device without camera, the app crashes (with an internal error) when the layout is being loaded:

B4X:
Root.LoadLayout("MainPage")

From the crash text, It seems thee QRCodeReaderView is crashing on the device (I suppose because it doesn't have camera).

Did anyone had the issue before? Is something I can do to only add the QRCodeReaderView to the layout when the device has camera? Can I know that the device has camera, or not?

If you know the issues isn't related with this, I would appreciate any oppinion.

Thanks a lot in advance.
 

Johan Schoeman

Expert
Licensed User
Longtime User
Hi all,

I'm using QRCodeReaderView in a project that wheen we activate a certain config flag, we can read QRcodes.

I've a QRCodeReaderView in my layout that I use when that flag is active.

The app works great in devices that has camera. When I install the app in a device without camera, the app crashes (with an internal error) when the layout is being loaded:

B4X:
Root.LoadLayout("MainPage")

From the crash text, It seems thee QRCodeReaderView is crashing on the device (I suppose because it doesn't have camera).

Did anyone had the issue before? Is something I can do to only add the QRCodeReaderView to the layout when the device has camera? Can I know that the device has camera, or not?

If you know the issues isn't related with this, I would appreciate any oppinion.

Thanks a lot in advance.
Why do you want to install the app on a device that does not have a camera? The purpose of the library is to scan QR Codes with a device that does have a camera.
 
Upvote 0

mmanso

Active Member
Licensed User
Longtime User
Hi there,

This app I'm developing is used in any device (with or without camera). We just have a certain feature that can be used in devices with cameras (on the others, it won't be used but the app is still relevant).
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
Hi there,

This app I'm developing is used in any device (with or without camera). We just have a certain feature that can be used in devices with cameras (on the others, it won't be used but the app is still relevant).
I guess you will have to add some code to check if the device does indeed have a camera.

If you add this to the B4A Manifest then it will probably warn you that your device does not have a camera when trying to download your app from the play store.

B4X:
AddManifestText(<uses-feature android:name="android.hardware.camera" android:required="true" />)
 
Upvote 0

teddybear

Well-Known Member
Licensed User
Hi there,

This app I'm developing is used in any device (with or without camera). We just have a certain feature that can be used in devices with cameras (on the others, it won't be used but the app is still relevant).
Prepare 2 sets of layouts for the app, detect if the camera is present on the phone and then load the different layout.
 
Upvote 0

toby

Well-Known Member
Licensed User
Longtime User
Try the tested code below:
B4X:
'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    If CheckCamera Then
        Root.LoadLayout("MainPage")
    Else
        Root.LoadLayout("MainPage1") 'without new qrcreaderview
    End If     
    
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Sub CheckCamera As Boolean
    Dim jo As JavaObject
    jo.InitializeContext
    Dim pm As JavaObject = jo.RunMethod("getPackageManager", Null)
    Return pm.RunMethod("hasSystemFeature", Array("android.hardware.camera"))
End Sub
 
Upvote 0

mmanso

Active Member
Licensed User
Longtime User
Try the tested code below:
B4X:
'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    If CheckCamera Then
        Root.LoadLayout("MainPage")
    Else
        Root.LoadLayout("MainPage1") 'without new qrcreaderview
    End If    
   
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Sub CheckCamera As Boolean
    Dim jo As JavaObject
    jo.InitializeContext
    Dim pm As JavaObject = jo.RunMethod("getPackageManager", Null)
    Return pm.RunMethod("hasSystemFeature", Array("android.hardware.camera"))
End Sub

This works Great.

I'll have to have two different layout files which are almost the same (one not having the qrcode view) but that's the minor of my problems.

Thanks a lot!
 
Upvote 0
Top