Android Question [Solved] USB External Storage Read Write

T201016

Active Member
Licensed User
Longtime User
Hi everyone!

I looked at some interesting posts related to USB file access.
I am interested in whether there is a way for me to be able to read the contents of the text file
from a USB stick (without copying the file), and send a new data stream to the same
a file on a USB flash drive?

I am only interested in reading and writing to the same file on a USB stick.
 

DonManfred

Expert
Licensed User
Longtime User
 
Upvote 0

T201016

Active Member
Licensed User
Longtime User
Thanks for the guidance but read this post before ...

I meant it
How to read a file as bytes from a USB stick and how to write bytes to a USB stick.
 
Upvote 0

T201016

Active Member
Licensed User
Longtime User
ExternalStorage class uses streams to read and write files. All you need is in the ExternalStorage class. You use OpenInputStream to read a file and OpenOutputStream to write to a file.

Thanks @agraham
I just solved the problem.
I thought it couldn't be that simple
and yet :)

Example: USB Read & Write:
#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
 
Upvote 0

T201016

Active Member
Licensed User
Longtime User

Following my earlier suggestions from other posts, I thought that the "intermediate" file should be used when I want to save something to a USB drive.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE) Wait For Activity_PermissionResult(Permission As String, Result As Boolean)
I don;t think you need runtimepermissions. But even if you do, this line is not correct:
Wait For Activity_PermissionResult(Permission As String, Result As Boolean)
It should be:
B4X:
 Wait For B4XPage_PermissionResult(Permission As String, Result As Boolean)
But, you said you solved it. What do I know. SInce you are posting too much code, it is probably more helpful to others if you attach your project or a stripped down version representing the above code.
 
Upvote 0

T201016

Active Member
Licensed User
Longtime User
I don;t think you need runtimepermissions. But even if you do, this line is not correct:
Wait For Activity_PermissionResult(Permission As String, Result As Boolean)
It should be:
B4X:
 Wait For B4XPage_PermissionResult(Permission As String, Result As Boolean)
But, you said you solved it. What do I know. SInce you are posting too much code, it is probably more helpful to others if you attach your project or a stripped down version representing the above code.

Welcome :)
Thank you for your interest and for the tip above.
The excerpt from my example in previous posts was taken from my own project, so it could contain some redundant code. You're right, in due course I'll try to include a truncated code draft.
Have a nice day.
 
Upvote 0

T201016

Active Member
Licensed User
Longtime User
But, you said you solved it. What do I know. SInce you are posting too much code, it is probably more helpful to others if you attach your project or a stripped down version representing the above code.

As suggested: @agraham and @Mahares ;)
ExternalStorage class uses streams to read and write files. All you need is in the ExternalStorage class. You use OpenInputStream to read a file and OpenOutputStream to write to a file.
I enclose a short application example.
 

Attachments

  • Simple.USB.zip
    5.2 KB · Views: 243
Upvote 0
Top