Android Question B4X runtime permission error

laviniut

Active Member
Licensed User
Longtime User
I used an example from xchart from Klaus to make a snapshot of a graph.
I used this code
B4X:
Sub savesnapshot
    Dim rp As RuntimePermissions
    rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result Then
        'access here
        Dim bmp As B4XBitmap = SingleLine1.Snapshot
        Dim out As OutputStream

        Dim mydir As String
        mydir = File.DirRootExternal & "/DPF Graph Snapshots/"
'    out = File.OpenOutput(File.DirRootExternal, "snap.png", False)
        out = File.OpenOutput(mydir, "snap.png", False)
        bmp.WriteToStream(out, 100, "PNG")
        out.Close
    Else
        'no permission
    End If

End Sub
and i get an error
java.lang.Exception: Sub activity_permissionresult was not found.

So I put this
B4X:
Sub Activity_PermissionResult (Permission As String, Result As Boolean)

End Sub
and error was the same
java.lang.Exception: Sub activity_permissionresult was not found.

Please help me.
Attached are test project.

Here are the log:

** Activity (main) Pause, UserClosed = false **
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
java.lang.Exception: Sub activity_permissionresult was not found.
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:219)
at anywheresoftware.b4a.BA$2.run(BA.java:387)
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:6131)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:892)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:782)
** Activity (main) Resume **
 

Attachments

  • Test project ChartsDemo.zip
    15.5 KB · Views: 270
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Activity_PermissionResult is an activity event so it will be raised in the activity and not in the class.

You can add this to the main activity:
B4X:
Sub Activity_PermissionResult (Permission As String, Result As Boolean)
 CallSubDelayed3(YourClassInstance, "Activity_PermissionResult", Permission, Result)
End Sub

If you are working on a new project then I recommend you to switch to B4XPages. This makes everything simpler including the handling of the runtime permissions.
 
Upvote 0

laviniut

Active Member
Licensed User
Longtime User
Thank you. I use B4XPages. I put it in main activity and is ok, but now i get an error
java.io.FileNotFoundException: /storage/emulated/0/DPF Graph Snapshots/snap657571992020.png (No such file or directory)
I supose i need to create this directory in main at first time or i can test if directory exist in snapshot sub and make this dir.
How can i test if dir exist?
 
Last edited:
Upvote 0

laviniut

Active Member
Licensed User
Longtime User
Thank you. Now i resolved.

If is useful for somebody, this is the code

B4X:
Sub savesnapshot
    Dim rp As RuntimePermissions
    rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result Then
        'access here
        Dim snapname As String
        snapname = "snap"&DateTime.GetHour(DateTime.Now) & DateTime.GetMinute(DateTime.Now) & DateTime.GetSecond(DateTime.Now) & DateTime.GetDayOfYear(DateTime.Now) &  DateTime.GetYear(DateTime.Now)&".png"
        Log("numele: " & snapname)
        Dim bmp As B4XBitmap = SingleLine1.Snapshot
        Dim out As OutputStream
        
        Dim mydir As String
        mydir = File.DirRootExternal & "/DPF Graph Snapshots/"
        If File.Exists(mydir,"") Then
            out = File.OpenOutput(mydir, snapname, False)
            bmp.WriteToStream(out, 100, "PNG")
            out.Close
            Else
            File.MakeDir(File.DirRootExternal, "/DPF Graph Snapshots/")
            out = File.OpenOutput(mydir, snapname, False)
            bmp.WriteToStream(out, 100, "PNG")
            out.Close
        End If
'    out = File.OpenOutput(File.DirRootExternal, "snap.png", False)
        
    Else
        'no permission
    End If
    
End Sub

and
B4X:
Sub Activity_PermissionResult (Permission As String, Result As Boolean)
    CallSubDelayed3(XM, "Activity_PermissionResult", Permission, Result)
End Sub
in main activity.

Again, thank you.
 
Upvote 0
Top