Saving a file from HttpUtils

NeoTechni

Well-Known Member
Licensed User
Longtime User
B4X:
Sub SaveFile(URL As String, Dir As String , Filename As String)
   Dim  FS As OutputStream ,Iss As InputStream, Buffer(1024) As Byte , BytesRead As Int ,Start As Int ,Cache As Int
   FS= File.OpenOutput(Dir, Filename  , False )  
   Iss = HttpUtils.GetInputStream(URL)
   Start=0
   Do Until Bytesread = -1
      BytesRead = iss.ReadBytes(  Buffer, 0 , Buffer.Length  )
      If bytesread>-1 Then Cache = bytesread
      FS.WriteBytes(Buffer, 0, Cache)
      start=start+ BytesRead
   Loop
   iss.Close 
   fs.Close 
End Sub

I keep getting an error on "BytesRead = iss.ReadBytes( Buffer, start , Buffer.Length )", it says start is out of bounds after the first read (so start=1024 at this point) I'd try 0, but that doesnt make sense, it should keep resaving the first 1024 bytes.

I don't want to use GetBitmap, then resave it as a jpg (cause that'd add more compression artifacts to the thumbnail) or a bmp (larger filesize)
 
Last edited:

NeoTechni

Well-Known Member
Licensed User
Longtime User
I tried 0 and it worked.

I would recommend that sub be moved to the library as it's quite handy
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
start is out of bounds after the first read (so start=1024 at this point) I'd try 0, but that doesnt make sense
It does because the StartOffset parameter in ReadBytes defines where to save the data in the buffer, not where to start in the stream.
 
Upvote 0
Top