Android Code Snippet External Storage - Simple Example

Enclosed is a simple call to storage , creating a directory and copying files from DirInternal using ExternalStorage Class
Distilled from several sources, especially Manfred's work.


B4X:
'Creates folder in the destination folder selected
'Copies file from DirInternal
Sub FiletoExtStorage(DirName As String,FileToCopy As String)
        Dim inpstr As InputStream          = File.OpenInput(File.DirInternal,FileToCopy)         'Create an Inputstream from the Sourcefile to copy

        Dim DirName1 As ExternalFile     = Storage.FindDirOrCreate(Storage.Root, DirName)       'creates the folder
        Dim destfile As ExternalFile     = Storage.CreateNewFile(DirName1,FileToCopy)           'create the file
        Dim os As OutputStream             = Storage.OpenOutputStream(destfile)                    'Create an Outputstream to the destfile
        
        File.Copy2(inpstr,os)                                                                  'Copy file
        inpstr.Close                                                                        'Close inputstr
        os.Close                                                                              'Close Outputstream
End Sub


Sub SetTestFiles
    FileName1= "RodTest1.txt"
    File.WriteString(File.DirInternal, FileName1, "jaklsdjalksdjalskdjasld")               
    
    Filename2= "RodTest2.txt"
    File.WriteString(File.DirInternal, Filename2, "qwertyqwerty")               
    
End Sub


Sub btnTest_Click
    SetTestFiles
    Storage.SelectDir(True)
    Wait For Storage_ExternalFolderAvailable
    FiletoExtStorage("Terminations",FileName1)            ' Directory and File to Send
    FiletoExtStorage("Terminations",Filename2)            ' Directory and File to Send
End Sub

Added this to Class which was from another contributor
B4X:
Public Sub FindDirOrCreate(Parent As ExternalFile, Name As String) As ExternalFile
    Dim f As ExternalFile = FindFile(Parent, Name)
    If f.IsInitialized = False Then
        Return DocumentFileToExternalFile(Parent.Native.RunMethod("createDirectory", Array(Name)))
    Else
        Return f
    End If
End Sub
 

Toky Olivier

Active Member
Licensed User
Longtime User
Thank you. But as It's a code snippet, isn't it better to add also needed Permission and it's checking as you write in the External storage?
 

rodmcm

Active Member
Licensed User
Yes
There are a lot of other items you could also add such as checks that the folder and file are in fact written. All I wanted to do with this snippet is to present an easy to understand method which works
 

rodmcm

Active Member
Licensed User
Yes it does work,

It is totally based on the ExternalStorage class. The storage.selectdir(true) invokes the selection of the directory/folder to select as per Manfreds example

This could have been included in the subroutine as well.

All I have done is provided a simple solution to a question often asked on the forum. As you have told many people other methods don't work but no-one has put a simple solution up and certainly I wanted to reduce it to a simple format.

The reason I have embedded File.FileInternal as it is the most likely place APPs place data before export.

It could of course been any directory as per the normal file copy function
 

rodmcm

Active Member
Licensed User
Yes,
It is really sad that Android hasn't got their act together on file storage etc. The last thing I want a user of my App to do is mess around in the files of external storage, I would rather my App can predetermine the location.... Maybe next year.......
 

mangyun

Member
@rodmcm, i tried your sample, but can't figured out how to change it so it will not create new directory.
How to copy the file to the root folder picked by user and overwrite them.

any tips ?
 
Last edited:

mangyun

Member
@rodmcm, i tried your sample, but can't figured out how to change it so it will not create new directory.
How to copy the file to the root folder picked by user and overwrite them.

any tips ?

sorry, please ignore my question, got stucked these couple of days.
i'll just use the folder created, and already found a way to delete the file before creating them.

delete:
    Dim extfile As ExternalFile = Storage.FindFile(DirName1, FileToCopy)
    Storage.DeleteFile(extfile)
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…