#Region Shared Files
#End Region
Sub Class_Globals
Public rp As RuntimePermissions
Private Root As B4XView
Public Storage As ExternalStorage
Public randomfile As ExternalFile
Private EditText1 As EditText
End Sub
Public Sub Initialize
' B4XPages.GetManager.LogEvents = True
End Sub
'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
Root = Root1
Root.LoadLayout("MainPage")
rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
Wait For Activity_PermissionResult(Permission As String, Result As Boolean)
ToastMessageShow("Result: "&Result, True)
End Sub
'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.
Private Sub Button1_Click
Storage.Initialize (Me, "Storage")
B4XPages.MainPage.Storage.SelectDir(False)
Wait For Storage_ExternalFolderAvailable
Try
B4XPages.MainPage.randomfile.Initialize
B4XPages.MainPage.randomfile = B4XPages.MainPage.Storage.FindDirOrCreate(B4XPages.MainPage.Storage.Root,"RandomFile.txt")
#Region - READING content from an existing file (RandomFile.txt):
'----------------------------------------------------------------
If B4XPages.MainPage.randomfile.IsInitialized Then
Dim inputstream As InputStream = B4XPages.MainPage.Storage.OpenInputStream(B4XPages.MainPage.randomfile)
Dim buffer() As Byte = Bit.InputStreamToBytes(inputstream)
Dim s As String = BytesToString(buffer, 0, buffer.Length, "UTF8")
EditText1.Text = s
End If
'----------------------------------------------------------------
#End Region
Catch
MsgboxAsync(LastException.Message,"ERROR-READING")
End Try
End Sub
Private Sub Button2_Click
Try
#Region - SAVING content to an existing file (RandomFile.txt):
'----------------------------------------------------------------
If B4XPages.MainPage.randomfile.IsInitialized Then
EditText1.Text = "SAVING content to an existing file (RandomFile.txt)"
Dim buffer() As Byte = EditText1.Text.GetBytes("UTF8")
Dim outputstream As OutputStream = B4XPages.MainPage.Storage.OpenOutputStream(B4XPages.MainPage.randomfile)
Dim inputstream As InputStream
inputstream.InitializeFromBytesArray(buffer,0,buffer.Length)
' Save file
Wait For (File.Copy2Async(inputstream, outputstream)) Complete (Success As Boolean)
ToastMessageShow("Success SAVING: " & Success, True)
' Close Outputstream
outputstream.Close
End If
'----------------------------------------------------------------
#End Region
Catch
MsgboxAsync(LastException.Message,"ERROR-SAVING")
End Try
End Sub