Android Question Using list to receive AsyncStream - How to display or save it ?

dim

Member
Licensed User
Longtime User
Hello,
I am trying to receive a jpeg image from an application running on PC and I use AsyncStreams for that.
Since I cannot use prefix mode, the image comes in chunks on the android application.
So "_NewData" event is called many times and I need to gather all parts of jpeg and finally concat them.
I am trying to do it using lists.
So my code is like :
B4X:
...
Dim input_list As List
...
input_list.initialize
...
Sub stream1_NewData (buffer() As Byte)
...
input_list.AddAll(buffer)
...
End Sub
In order to understand the end of file for jpeg, I read the last two bytes of every chunk and when these last two bytes are FFD9 (hex) then I suppose it's the correct end of file for Jpeg.

The problem is that when all the jpeg image has been received in input_list, then I cannot find out how to use this list to save the image in jpeg format or to display the image on an imageview.

Can please someone help ?

Thank you
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I read the last two bytes of every chunk and when these last two bytes are FFD9 (hex)
This is not a very reliable check. It is also inefficient as the number of bytes can be very large. Why don't you use B4J to implement the PC side? It will be much simpler.

There is no simple / efficient way to convert the data to a bytes array. You can instead save the new data to a file.
 
Upvote 0

dim

Member
Licensed User
Longtime User
Thank you Erel,

I don't use B4J because what I am working on is an old Lazarus-Pascal based project using many other components and it would need huge work to transfer it to B4J. I am now trying to extend this project with support for cameras that's why I also try to find a solution using B4A for the mobile part.
The images are small JPEGs, not more than 100KB. The check I do with FFD9 may not be very reliable but since I cannot use prefix mode, I cannot find any other way.
I have searched the B4A forums for days but also did not find something that could help.
JPEGs (on the PC side) are generated by my code (they are screenshots received from a camera) and having checked thousands of them, I found that all of them have that FFD9 in the end of the file, so I estimate that it may be OK.

Saving the data to a file is not efficient either because I would need to do repeat it fast enough (about 4-5 times per second or even more). I think it would cause problems.
But anyway, how could I save the new data (binary) from a list to a file ?
This code works if I have a buffer (array), but when having a list what could I use ?
Generally, since an array of bytes can be moved to a list for better handling (like extending), why there is no easy way to do the opposite, meaning transfer a list having elements of bytes to an array (buffer) ?
B4X:
Dim out As OutputStream
counter2 = counter2 + 1
out = File.OpenOutput(File.DirRootExternal, "scr_" & counter2 & ".jpg", False)
out.WriteBytes(input_buffer, 0, input_buffer.Length)
out.Close

Finally, before using list, I tried using ArrayCopy but had other problems there : Before calling ArrayCopy had to Dim the total_buffer (that would hold the final image) with the size of the current total_buffer + buffer, but beause of the "Dim" the total_buffer was initialized and lost all previous bufferings.

Any help on these subjects would be greatly appreaciated !
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
the total_buffer was initialized and lost all previous bufferings.
sounds like you need to write a small helper sub which
- get the total_buffer plus the additiona data as parameter
- dim a new array with a size of total-buffer+newbuffer
- copy the total buffer to the new array
- copy the aditional buffer to the array
- return the new array
 
Upvote 0

dim

Member
Licensed User
Longtime User
Thank you DonManfred - you are absolutely right.
But I keep trying to find a solution using lists instead of arrays because (I think) using lists would be much faster.
It's faster to "add" to a list in order to extend it than to use copying arrays of at least 50K - is that right ?
At least it would be efficient to only handle with arrays at the final stage in order to transfer the final list back to an array, to be able to continue with saving it to a file or displaying the image on the imageview.
Any idea on that ?
Thanks a lot, anyway !
 
Upvote 0
Top