Android Question RuntimePermissions not prompting

Shivito1

Active Member
Licensed User
My app was fine but recently needed to target SDK 26 and now my app is not working on new devices.
B4X:
Sub Process_Globals
   'Get permission to write file
   Private rp As RuntimePermissions
End Sub
Sub Activity_Create(FirstTime As Boolean)
    rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
 

Shivito1

Active Member
Licensed User
I don't see how we can help you with the information you provided. Sorry.
The error listed below tells me i don't have permission to write/read from the file location. After searching I found I need to use RuntimePermissions in order to fix this error. However I must be doing something wrong because when I debug I still get Permission denied. Also I am never prompted to give permission.
My Code:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
    infocheck1.Initialize("")
    If File.Exists(File.DirRootExternal &"/extractsrt/", "infocheck.txt") == False Then
        File.WriteString(File.DirRootExternal &"/extractsrt/", "infocheck.txt","0")
        
    else if File.Exists(File.DirRootExternal &"/extractsrt/", "infocheck.txt") == True Then
        Log(File.ReadList(File.DirRootExternal &"/extractsrt/", "infocheck.txt"))
    End If
Error:
B4X:
Error occurred on line: 685 (Main)
java.io.FileNotFoundException: /storage/emulated/0/extractsrt/infocheck.txt (Permission denied)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:200)
    at java.io.FileInputStream.<init>(FileInputStream.java:150)
    at anywheresoftware.b4a.objects.streams.File.OpenInput(File.java:209)
    at anywheresoftware.b4a.objects.streams.File.ReadList(File.java:233)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
    at com.subreader.main.afterFirstLayout(main.java:102)
    at com.subreader.main.access$000(main.java:17)
    at com.subreader.main$WaitForLayout.run(main.java:80)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6960)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
 

Xicu

Active Member
Licensed User
Use the event sub
B4X:
Sub Process_Globals
   'Get permission to write file
   Private rp As RuntimePermissions
End Sub
Sub Activity_Create(FirstTime As Boolean)
    rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
End Sub

'Event sub example
Sub Activity_PermissionResult (Permission As String, Result As Boolean)
  
    Select Case Permission
        Case rp.PERMISSION_WRITE_EXTERNAL_STORAGE
            If Result = True Then
                infocheck1.Initialize("")
                If File.Exists(File.DirRootExternal &"/extractsrt/", "infocheck.txt") == False Then
                    File.WriteString(File.DirRootExternal &"/extractsrt/", "infocheck.txt","0")
    
                else if File.Exists(File.DirRootExternal &"/extractsrt/", "infocheck.txt") == True Then
                    Log(File.ReadList(File.DirRootExternal &"/extractsrt/", "infocheck.txt"))
                End If
            Else
                ToastMessageShow("You have not permission to write external storage", True) 
            End If
        End If
    End Select
End Sub
 
Last edited:

Shivito1

Active Member
Licensed User
Thanks all!
I got it to ask but only if i debug and step through the code if I don't it finishes the Activity Create sub before getting permission. I'm currently trying to re write my code so that activity create can finish before the write/read is needed but I'm not having much luck.

Is there a way to prompt for permission before it finishes the Activity_Create sub?
 
Last edited:

Shivito1

Active Member
Licensed User
Awesome I got it XD
Feels like a hack job but its working now.

So in my Activity_Create I needed the permission. My workaround was to use (Try Catch) along with a variable. then when the result of the permission request is checked the variable lets the program know if it needs to re run activity_create(True)

in any case. Thank you all very very much!
 
Top