Android Question Solved - Can multiple File.OpenOutput yield 1 file?

anOparator

Active Member
Licensed User
Longtime User
display image 1, then image 2, etc from db.
B4X:
Sub Button1_Click
If pos = 61 Then
   pos = 0
   ReadBlob
   prnt2                         '
Else
   pos = pos + 1
   ReadBlob
   prnt2                ' Save to file.
End If
End Sub
And save the OutputStream to file, ending up with all the images on SD Card.
B4X:
Sub prnt2
Dim Cursor3 As Cursor = SQL1.ExecQuery("select image FROM MyTable WHERE name = name")
  Cursor3.Position = pos
  Dim Bmp As Bitmap           '
     If Cursor3.RowCount > pos Then
  Dim binImage() As Byte, Otpt As InputStream
  binImage = Cursor3.GetBlob("Image")
  Otpt.InitializeFromBytesArray(binImage, 0, binImage.Length)
  Bmp.Initialize2(Otpt)
  Otpt.Close
  End If
  Cursor3.Close
     
Dim out As OutputStream = File.OpenOutput(File.DirDefaultExternal, pos & ".png" , True)  

out.WriteBytes(binImage, 0, binImage.Length)
out.Close
End Sub
I tried
B4X:
Dim out As OutputStream = File.OpenOutput(File.DirDefaultExternal, 8 & ".png" , True)
but ended up with only the first file named 8.png.

How would I have each OutputStream overwrite the previous one so that I always have only one file in DirDefaultExternal?
Something like the MS-DOS command prompt:
Dir > 1.png
Many thanks in advance for all tips.
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
i would try it like this.
B4X:
Sub prnt2
    Dim Cursor3 As Cursor = SQL1.ExecQuery("select image FROM MyTable WHERE name = name")
    Cursor3.Position = pos
    Dim Bmp As Bitmap           '
    If Cursor3.RowCount > pos Then
        Dim binImage() As Byte, Otpt As InputStream
        binImage = Cursor3.GetBlob("Image")
        Otpt.InitializeFromBytesArray(binImage, 0, binImage.Length)
        Bmp.Initialize2(Otpt)
        Otpt.Close
        Dim out As OutputStream = File.OpenOutput(File.DirDefaultExternal, pos & ".png" , True)
        Bmp.WriteToStream(out, 100, "JPEG") ' write the bitmap to the stream
        out.Close
    End If
    Cursor3.Close
End Sub
 
Upvote 0

anOparator

Active Member
Licensed User
Longtime User
i would try it like this.
B4X:
Sub prnt2
    Dim Cursor3 As Cursor = SQL1.ExecQuery("select image FROM MyTable WHERE name = name")
    Cursor3.Position = pos
    Dim Bmp As Bitmap           '
    If Cursor3.RowCount > pos Then
        Dim binImage() As Byte, Otpt As InputStream
        binImage = Cursor3.GetBlob("Image")
        Otpt.InitializeFromBytesArray(binImage, 0, binImage.Length)
        Bmp.Initialize2(Otpt)
        Otpt.Close
        Dim out As OutputStream = File.OpenOutput(File.DirDefaultExternal, pos & ".png" , True)
        Bmp.WriteToStream(out, 100, "JPEG") ' write the bitmap to the stream
        out.Close
    End If
    Cursor3.Close
End Sub
Thanks for the Tag reminder, the Sub should end with the starting Tag (Cursor3).
However all the images get written to SD card.

Is there a way to force
B4X:
Dim out As OutputStream = File.OpenOutput(File.DirDefaultExternal, pos & ".png" , True)
to overwrite the previously written file and only have the last seen file on SD card?
Yes
B4X:
Dim out As OutputStream = File.OpenOutput(File.DirDefaultExternal, "Picture" & ".png" , False)

Thanks, you're a genius. I will donate.
 
Upvote 0
Top