Android Question ExternalStorage Write/Read file

Discussion in 'Android Questions' started by MitchBu, May 17, 2019.

  1. MitchBu

    MitchBu Active Member Licensed 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.
     
  2. agraham

    agraham Expert Licensed 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.
    Code:
    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"""""NullTrue)
        
    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.
     
    MitchBu likes this.
  3. DonManfred

    DonManfred Expert Licensed User

    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/

    Code:
    ' 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
     
    agraham and MitchBu like this.
  4. MitchBu

    MitchBu Active Member Licensed User

    Thank you agraham and DonManfred.

    I shall experiment.

    Right now I have copied the class into my app, and am starting to understand it.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice