Android Question Intent based SD card folder picker: how to get the selected folder name

toby

Well-Known Member
Licensed User
Longtime User
My test app is based on the code herehttps://www.b4x.com/android/forum/t...-whole-of-external-sd-card.89798/#post-568065

Everything works except that I couldn't get the picked folder name. For example, if I select "test/data", I need "test/data" value be returned. The pickedDir (see quoted code below) returns something quite different, similar to: TreeDocumentFile) androidx.documentfile.provider.TreeDocumentFile@1007385

Could someone give me a hint on how to picked folder name I'm looking for?

Try this:
B4X:
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 JavaObject = jo.RunMethod("getData", Null)
       Dim ctxt As JavaObject
       ctxt.InitializeContext
       Dim DocumentFileStatic As JavaObject
       Dim pickedDir As JavaObject = DocumentFileStatic.InitializeStatic("android.support.v4.provider.DocumentFile").RunMethod("fromTreeUri", Array(ctxt, treeUri))
       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 If
   Return Null
End Sub


TIA
 

Attachments

  • TestfolderPickerIntent.zip
    9.8 KB · Views: 166
Last edited:

toby

Well-Known Member
Licensed User
Longtime User
I'll use the following code
B4X:
File.Copy(SourceDir, filenName, pickedDir, fileName)
to copy a file to the pickedDir on SD card.
 
Upvote 0

toby

Well-Known Member
Licensed User
Longtime User
I wouldn't need to know the picked folder name if I modify the code In the above ion_Event()
as shown below
B4X:
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
from creating a new file to copying an existing one.

Could someone show me how, please?

TIA
 
Upvote 0
Top