Android Question ExternalStorage Write/Read file

MitchBu

Well-Known Member
Licensed User
Longtime User
I am trying to wrap my head around writing to SD card, or reading file, so I can offer users to exchange data with my app.

Storage.CreateNewFile(Parent as ExternalFile, Name as String) as ExternalFile

I don't understand what to pass to the method as parent. Since SelectDir returns a string, how can I enter the parent ?

In the example program, GetCurrentFolder looks like what I would need, though.

I suppose this will take the result of the method above ?

Storage.OpenOutPutStream(EF as ExternalFile) as Outputstream.

Sorry for being thick. I went through the example program but could not find what I needed.

I would love a snippet showing how that is done.
 

agraham

Expert
Licensed User
Longtime User
This simplified Sub from my mapping application copies a map file from the SD Card to a folder on the internal (external!) storage.
The Folders parameter is a list of folders on the SD Card that comprise the folder tree to traverse.
ExternalFile can reference a folder as well as a file.
B4X:
Sub CopyMapToDownload(Folders() As String, Filename As String)
    Dim extfolder As ExternalFile
    Dim extfile As ExternalFile
 
    Storage.SelectDir(True)
    Wait For Storage_ExternalFolderAvailable
 
    Dim mapfound As Boolean = False
    Dim extfolder As ExternalFile = Storage.FindFile(Storage.Root, Folders(0))
    If Folders.Length > 1 Then
        For i = 1 To Folders.Length -1
            extfolder = Storage.FindFile(extfolder, Folders(i))
        Next
    End If
    If extfolder.IsInitialized Then
        Dim extfile As ExternalFile = Storage.FindFile(extfolder, Filename)
        If extfile.IsInitialized Then
            Dim out As OutputStream = File.OpenOutput(File.DirRootExternal, Starter.MapTempDestination, False)
            mapfound = True
        End If
    End If
    If Not (mapfound) Then
        Dim msg As String = "Map " & Filename & " not found!"
        Msgbox2Async(msg, "Missing Map", "OK", "", "", Null, True)
    End If    
End Sub
I haven't used CreateNewFile but you pass it an ExternalFile object for the folder in which to create the file as in File.FindFile above.
I don't think that you can create a new folder with the Storage class, you can only reference existing ones.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I don't think that you can create a new folder with the Storage class
Not directly but using Javaobject you can. Not all methods are implemented in the Class.

See https://www.b4x.com/android/forum/threads/copy-a-file-from-internal-memory-to-sd-card.102705/

B4X:
' Get DocumentDir
    Dim docs As JavaObject = GetDocumentdDir(PersistantUri)
  
    ' Check if Destfolder Test already exists.
    Dim chkTest As ExternalFile = Storage.FindFile(Storage.Root,"Test")
    If chkTest.IsInitialized = False Then
        'create the Folder "Test"
        docs.RunMethod("createDirectory",Array("Test"))
    End If
    ' Search the created Folder in the Storage Rootdir (we need to have a reference to it)
    Dim Test As ExternalFile = Storage.FindFile(Storage.Root,"Test")
  
    ' define destfile
    Dim destfile As ExternalFile = Storage.CreateNewFile(Test,filetocopy)
    ' Create an Outputstream to the destfile
    Dim os As OutputStream = Storage.OpenOutputStream(destfile)
    ' Create an Inputstream from the Sourcefile to copy
    Dim inpstr As InputStream  = File.OpenInput(File.Combine(File.DirRootExternal,"Download/"),filetocopy)
    ' Copy file
    File.Copy2(inpstr,os)
    ' Close Outputstream
    os.Close
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…