B4J Question How to open (run) a file

christiantric

Member
Licensed User
Hello Devs,
I have a custom type with a field Doc() As Byte where I store documents.
Supported types are txt, docx, xlsx, and images.
What I'm trying to do is:
- Get the file stream from my istance
- Save the file on file system
- Open the file with default system program (notepad for txt files, office word for docx, office excel for xlsx).

B4X:
Sub clvDocuments_ItemClick (Index As Int, Value As Object)
    
    Dim item As Document = documentRepo.Read(Value)
    
    If item.Doc <> Null And item.FileName.Length > 0 Then
        FileUtils.BytesToFile(File.DirTemp, item.Description, item.Doc)
        ' how to open the file now?!?
    End If
    
End Sub

How can I accomplish this?
Thanks in advance.
 

christiantric

Member
Licensed User
I don't know what is FileUtils however you don't need it. You can read and write bytes with File.ReadBytes / WriteBytes.

You can use fx.ShowExternalDocument to open files with the default app.

Thank you Erel,
this is exactly what I need.
I searched in the forum before open this thread but I wonder why I can't find a so simple solution.

In FileUtils I have this shorthand method:

B4X:
Sub BytesToFile (dir As String, fileName As String, data() As Byte)
    Dim out As OutputStream = File.OpenOutput(dir, fileName, False)
    out.WriteBytes(data, 0, data.Length)
    out.Close
End Sub

... but File.WriteBytes is even better ;-)
 
Upvote 0
Top