Android Question QR reader targetSdkVersion = "26" does not work

rscheel

Well-Known Member
Licensed User
Longtime User
I am using the QRCodeReaderView library, but when using the android: targetSdkVersion = "26" sends me the following error, I know that with this SDK I have to use RuntimePermissions but it is a problem of the library not the permissions, someone knows if there is another library which can use with SDK 26, instead works perfectly with SDK 22


B4X:
AddManifestText(<uses-feature android:name="android.hardware.telephony" android:required="false" />)
AddManifestText(<uses-feature android:name="android.hardware.camera" android:required="false" />)
AddManifestText(<uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />)
AddManifestText(<uses-feature android:name="android.hardware.camera.flash" android:required="false" />)
AddPermission("android.permission.INTERNET")
AddPermission("android.permission.ACCESS_FINE_LOCATION")
AddPermission("android.permission.WAKE_LOCK")
AddPermission("android.permission.DEVICE_POWER")
AddPermission("android.permission.READ_PHONE_STATE")
AddPermission("android.permission.VIBRATE")
AddPermission("android.permission.CAMERA")
AddPermission("android.permission.FLASHLIGHT")
AddPermission("android.hardware.camera")


B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
java.lang.RuntimeException: Fail to connect to camera service
    at android.hardware.Camera.<init>(Camera.java:575)
    at android.hardware.Camera.open(Camera.java:404)
    at com.google.zxing.client.android.camera.open.GingerbreadOpenCameraInterface.open(GingerbreadOpenCameraInterface.java:58)
    at com.google.zxing.client.android.camera.open.CameraManager.openDriver(CameraManager.java:81)
    at com.dlazaro66.qrcodereaderview.QRCodeReaderView.surfaceCreated(QRCodeReaderView.java:165)
    at android.view.SurfaceView.updateWindow(SurfaceView.java:618)
    at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:161)
    at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2205)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6344)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
    at android.view.Choreographer.doCallbacks(Choreographer.java:686)
    at android.view.Choreographer.doFrame(Choreographer.java:621)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6186)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
 

rscheel

Well-Known Member
Licensed User
Longtime User
Why are you setting the targetSdkVersion to 26? It is most probably a mistake.

Have you requested the permission at runtime?


I only intended to make an app with that sdk, but, if I have set the permissions at runtime, the problem is of the library not the permissions.

Although now I came back to sdk 22, since there it works well.

Thank you.
 
Upvote 0

supriono

Member
Licensed User
Longtime User
I only intended to make an app with that sdk, but, if I have set the permissions at runtime, the problem is of the library not the permissions.

Although now I came back to sdk 22, since there it works well.

Thank you.
same with me..you have any progres..
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
same with me..you have any progres..
It works if you add the required runtime permission to the B4A project

B4X:
#Region  Project Attributes
    #ApplicationLabel: QRCodeReaderView
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False

#End Region


#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    
    Dim nativeMe As JavaObject
    Dim rp As RuntimePermissions               'ADDED 8 June 2018

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    
    
    Private b1 As Button
    Private b2 As Button
    Private p1 As Panel
    Private l1 As Label
    Private oldString As String = ""
    
    Private b3 As Button
    

'    Private cb1 As CheckBox
    
    
    
    Private qrcrv As QRCodeReaderV102
    Private ImageView1 As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("main")
    nativeMe.InitializeContext
    
    
    qrcrv.Visible = False

    'Keep the order of the next 4 lines. CAMERA_BACK and CAMERA_FRONT will only contain correct values once qrcrv.BackFacingCamera and qrcrv.FrontFacingCamera has
    'been executed else they will have default values of -1
    Log(qrcrv.BackFacingCamera)    'see the popup help when entering this method - check if there is a back facing camera and return the ID of the camera
    Log(qrcrv.FrontFacingCamera)   'see the popup help when entering this method - check if there is a front facing camera and retutn the ID of the camera
    
    Log(qrcrv.CAMERA_BACK)         'see the popup help when entering this method
    Log(qrcrv.CAMERA_FRONT)           'see the popup help when entering this method
    
    'now you can use
    qrcrv.CameraId = qrcrv.CAMERA_BACK   'or qrcrv.CameraId = qrcrv.CAMERA_FRONT
    
    'qrcrv.CameraDegrees = 270  'set this to 0, 90, 180, 270
    
    qrcrv.ScanNow = True
    
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub b1_Click
    
    Dim Result As Boolean = True                                                                       'ADDED 8 June 2018
    If Not(rp.Check(rp.PERMISSION_CAMERA)) Then                                                        'ADDED 8 June 2018
        rp.CheckAndRequest(rp.PERMISSION_CAMERA)                                                       'ADDED 8 June 2018
        Wait For Activity_PermissionResult (Permission As String, Result As Boolean)                   'ADDED 8 June 2018
    End If                                                                                             'ADDED 8 June 2018
    If Result Then
      qrcrv.Visible = True
      Sleep(0)
      qrcrv.startScan
    End If 
      
End Sub

Sub b2_Click
    
    qrcrv.stopScan
    Sleep(0)
    qrcrv.Visible = False


End Sub

Sub qrcrv_result_found(retval As String, img As Object)
    If oldString <> retval Then
      oldString = retval
      Log("B4A: " & oldString)
      l1.Text = oldString   
      Dim bm As Bitmap = img
      ImageView1.Bitmap = bm
      nativeMe.RunMethod("playTone", Null)
      
    End If
    
End Sub

Sub b3_Click
    
    If (qrcrv.isFlashOn = True) Then
      qrcrv.setFlashOff
    Else
      qrcrv.setFlashOn
    End If 
    
End Sub

'Sub cb1_CheckedChange(Checked As Boolean)
'   
'    If cb1.Checked = True Then
'        qrcrv.ScanNow = True
'    Else
'        qrcrv.ScanNow = False
'    End If
'   
'End Sub

#if Java

import android.media.ToneGenerator;
import android.media.AudioManager;

  public void playTone() {
      final ToneGenerator tg = new ToneGenerator(AudioManager.STREAM_NOTIFICATION, 100);
      tg.startTone(ToneGenerator.TONE_PROP_BEEP);
  }     

#End If
 

Attachments

  • QRCodeReaderV102LibFiles.zip
    32.7 KB · Views: 78
  • b4aQRCodeReaderView.zip
    10.6 KB · Views: 84
Upvote 0

supriono

Member
Licensed User
Longtime User
It works if you add the required runtime permission to the B4A project

B4X:
#Region  Project Attributes
    #ApplicationLabel: QRCodeReaderView
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False

#End Region


#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
   
    Dim nativeMe As JavaObject
    Dim rp As RuntimePermissions               'ADDED 8 June 2018

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
   
   
    Private b1 As Button
    Private b2 As Button
    Private p1 As Panel
    Private l1 As Label
    Private oldString As String = ""
   
    Private b3 As Button
   

'    Private cb1 As CheckBox
   
   
   
    Private qrcrv As QRCodeReaderV102
    Private ImageView1 As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("main")
    nativeMe.InitializeContext
   
   
    qrcrv.Visible = False

    'Keep the order of the next 4 lines. CAMERA_BACK and CAMERA_FRONT will only contain correct values once qrcrv.BackFacingCamera and qrcrv.FrontFacingCamera has
    'been executed else they will have default values of -1
    Log(qrcrv.BackFacingCamera)    'see the popup help when entering this method - check if there is a back facing camera and return the ID of the camera
    Log(qrcrv.FrontFacingCamera)   'see the popup help when entering this method - check if there is a front facing camera and retutn the ID of the camera
   
    Log(qrcrv.CAMERA_BACK)         'see the popup help when entering this method
    Log(qrcrv.CAMERA_FRONT)           'see the popup help when entering this method
   
    'now you can use
    qrcrv.CameraId = qrcrv.CAMERA_BACK   'or qrcrv.CameraId = qrcrv.CAMERA_FRONT
   
    'qrcrv.CameraDegrees = 270  'set this to 0, 90, 180, 270
   
    qrcrv.ScanNow = True
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub b1_Click
   
    Dim Result As Boolean = True                                                                       'ADDED 8 June 2018
    If Not(rp.Check(rp.PERMISSION_CAMERA)) Then                                                        'ADDED 8 June 2018
        rp.CheckAndRequest(rp.PERMISSION_CAMERA)                                                       'ADDED 8 June 2018
        Wait For Activity_PermissionResult (Permission As String, Result As Boolean)                   'ADDED 8 June 2018
    End If                                                                                             'ADDED 8 June 2018
    If Result Then
      qrcrv.Visible = True
      Sleep(0)
      qrcrv.startScan
    End If
     
End Sub

Sub b2_Click
   
    qrcrv.stopScan
    Sleep(0)
    qrcrv.Visible = False


End Sub

Sub qrcrv_result_found(retval As String, img As Object)
    If oldString <> retval Then
      oldString = retval
      Log("B4A: " & oldString)
      l1.Text = oldString  
      Dim bm As Bitmap = img
      ImageView1.Bitmap = bm
      nativeMe.RunMethod("playTone", Null)
     
    End If
   
End Sub

Sub b3_Click
   
    If (qrcrv.isFlashOn = True) Then
      qrcrv.setFlashOff
    Else
      qrcrv.setFlashOn
    End If
   
End Sub

'Sub cb1_CheckedChange(Checked As Boolean)
'  
'    If cb1.Checked = True Then
'        qrcrv.ScanNow = True
'    Else
'        qrcrv.ScanNow = False
'    End If
'  
'End Sub

#if Java

import android.media.ToneGenerator;
import android.media.AudioManager;

  public void playTone() {
      final ToneGenerator tg = new ToneGenerator(AudioManager.STREAM_NOTIFICATION, 100);
      tg.startTone(ToneGenerator.TONE_PROP_BEEP);
  }    

#End If
thanks bro i will try.
 
Upvote 0
Top