#Region Project Attributes
#ApplicationLabel: SDCard File Management
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: portrait
#CanInstallToExternalStorage: False
#End Region
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
#AdditionalJar: androidx.legacy:legacy-support-core-utils
Sub Process_Globals
Public Storage As ExternalStorage
End Sub
Sub Globals
End Sub
Sub Activity_Create(FirstTime As Boolean)
'##########################################################
' Append data to a file on external device using ExternalStorage class
' - Create a file on external device in preselected folder (here root)
' - Output some text to it
' - Append additional text to this file
'##########################################################
Private cCSV As String, cFileDir As String = "TestDir", cFileCSV As String = "TestData.csv"
Private cStoragePath As ExternalFile, extFile As ExternalFile, storedFile As ExternalFile
Private DestinationFile As ExternalFile
Private outputstream As OutputStream, inputstream As InputStream
'##########################################################
' Initialization and test basic file operations
'##########################################################
' Storage initialization for external device
Storage.Initialize (Me, "Storage")
' Wait until external system is available and call user prompt if not
Storage.SelectDir(True)
Wait For Storage_ExternalFolderAvailable
' Find a file on external storage or create it
cStoragePath = Storage.Root
extFile = Storage.FindDirOrCreate(cStoragePath, cFileDir)
' Find a file in selected folder and delete it if existing
storedFile = Storage.FindFile(extFile,cFileCSV)
If storedFile.IsInitialized = True Then
Storage.DeleteFile(storedFile)
End If
'##########################################################
' Write data first time to external device
'##########################################################
' Copy a string to inputstream
cCSV = "Header1;Header2" & CRLF & "Value11;Value12" & CRLF & "Value21;Value22"
inputstream = StringToInputStream (cCSV)
' Allocate a new file on external device
DestinationFile = Storage.CreateNewFile(extFile,cFileCSV)
outputstream = Storage.OpenOutputStream(DestinationFile)
' Copy new file to external device
File.Copy2(inputstream,outputstream)
' Close streams
outputstream.Close
inputstream.Close
Log ("File '" & cFileCSV & "' created in folder '" & cFileDir & "'")
Log ("Content of 'cCSV' written to File '" & cFileCSV & "'")
'##########################################################
' Append data to already existing file on external device
'##########################################################
' Copy a string to inputstream
cCSV = CRLF & "Value31;Value32"
inputstream = StringToInputStream (cCSV)
' Open file on external device in append modus
outputstream = OpenOutputStreamAppend(DestinationFile)
' Copy data to external device
File.Copy2(inputstream,outputstream)
' Close streams
outputstream.Close
inputstream.Close
Log ("Content of updated 'cCSV' appended to file '" & cFileCSV & "' in folder '" & cFileDir & "'")
' Close app
Log ("Done")
Activity.Finish
ExitApplication
End Sub
Sub StringToInputStream (s As String) As InputStream
' Copy string to inputstream
Dim In As InputStream
Dim data() As Byte = s.GetBytes("UTF8")
In.InitializeFromBytesArray(data, 0, data.Length)
Return In
End Sub
Public Sub OpenOutputStreamAppend (EF As ExternalFile) As OutputStream
' Open outputstream in append modus
Dim ctxt As JavaObject
ctxt.InitializeContext
Return ctxt.RunMethodJO("getContentResolver", Null).RunMethod("openOutputStream", Array(EF.Native.RunMethod("getUri", Null), "wa"))
End Sub