Android Question [B4X] B4XPages - StartActivityForResult camera failing

Alessandro71

Well-Known Member
Licensed User
Longtime User
While converting a project to B4XPages, the following code, that worked fine before, is failing on the StartActivityForResult, getting trapped by Catch clause, triggering the "camera not available" toast message.
Since it comes straight from an old example from the forum, and was working before, I suspect a different handling of the CreateEvent part, but I'm unable to sort it out.

B4X:
#region photo
Sub TakePicture(frame As PhotoFrame)
    WriteLog("starting camera")
    currentPhotoFrame = frame
    
    Dim i As Intent
    i.Initialize("android.media.action.IMAGE_CAPTURE", "")
    File.Delete(Starter.Provider.SharedFolder, frame.ImageFilename)
    Dim u As Object = Starter.provider.GetFileUri(frame.ImageFilename)
    i.PutExtra("output", u) 'the image will be saved to this path
    Try
        StartActivityForResult(i)
    Catch
        ExceptionHandler.Manage("Wizard.TakePicture", ExceptionHandler.EXCEPTION_NONCRITICAL)
        ToastMessage.Show("Camera is not available.", ToastMessage.TOAST_ERR)
        WriteLog("error opening camera")
    End Try
End Sub

'result arrives here
Sub ion_Event (MethodName As String, Args() As Object) As Object
    If -1 = Args(0) Then
        Try
            If File.Exists(Starter.provider.SharedFolder, currentPhotoFrame.ImageFilename) Then
                currentPhotoFrame.LoadImage
            Else
                WriteLog("no filename returned from camera")
            End If
        Catch
            ExceptionHandler.Manage("Wizard.TakePicture_Event", ExceptionHandler.EXCEPTION_NONCRITICAL)
            WriteLog("event error")
        End Try
    End If
    Return Null
End Sub

Sub StartActivityForResult(i As Intent)
    Dim jo As JavaObject = GetBA
    ion = jo.CreateEvent("anywheresoftware.b4a.IOnActivityResult", "ion", Null)
    jo.RunMethod("startActivityForResult", Array As Object(ion, i))
End Sub

Sub GetBA As Object
    Dim jo As JavaObject
    Dim cls As String = Me
    cls = cls.SubString("class ".Length)
    jo.InitializeStatic(cls)
    Return jo.GetField("processBA")
End Sub
#End Region
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
 
Upvote 0
Top