Sub Activity_Click
Dim i As Intent
i.Initialize("android.intent.action.OPEN_DOCUMENT_TREE", "")
StartActivityForResult(i)
End Sub
Sub ion_Event (MethodName As String, Args() As Object) As Object
If Args(0) = -1 Then 'resultCode = RESULT_OK
Dim i As Intent = Args(1)
Dim jo As JavaObject = i
Dim treeUri As Uri = jo.RunMethod("getData", Null)
Dim ctxt As JavaObject
ctxt.InitializeContext
Dim takeFlags As Int = Bit.And(i.Flags, 3)
ctxt.RunMethodJO("getContentResolver", Null).RunMethod("takePersistableUriPermission", Array(treeUri, takeFlags))
Dim uri As String = treeUri 'ignore
'uri should be a process global variable and you can save it
End If
Return Null
End Sub
Sub GetPickedDir (uri As String) As JavaObject
Dim ctxt As JavaObject
ctxt.InitializeContext
Dim DocumentFileStatic As JavaObject
Dim treeUri As Uri
treeUri.Parse(uri)
Dim pickedDir As JavaObject = DocumentFileStatic.InitializeStatic("android.support.v4.provider.DocumentFile").RunMethod("fromTreeUri", Array(ctxt, treeUri))
Return pickedDir
'usage example of pickedDir
' Dim files() As Object = pickedDir.RunMethod("listFiles", Null)
' For Each f As JavaObject In files
' Log(f.RunMethod("getName", Null))
' Next
' Dim newFile As JavaObject = pickedDir.RunMethod("createFile", Array("text/plain", "My Novel"))
' Dim out As OutputStream = ctxt.RunMethodJO("getContentResolver", Null).RunMethod("openOutputStream", Array(newFile.RunMethod("getUri", Null)))
' Dim tw As TextWriter
' tw.Initialize(out)
' tw.Write("this is a test")
' tw.Close
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