Android Question Problem receiving long data via AStreams

JoanRPM

Active Member
Licensed User
Longtime User
Hello.
I am making an application using sockets to send data (text, pictures, etc).
The PC is the server and Android the client.
I use AStreams prefix mode since it can receive more than one package.

If the server sends few data (up to 750KB), it works correctly. I can display the JPG correctly.
But if the data is higher, I get an error "OutOfMemoryError" in B4A.
I have looked at how much RAM do I have, and I have more than 350MB.

My piece of program is as follows:
B4X:
msg = BytesToString(Buffer, 0, Buffer.Length, "ASCII")
lvLog.AddSingleLine (msg)
Log(msg)

Dim aux() As Byte
aux = b64.DecodeBtoB(Buffer, 0, Buffer.Length)
lblMsg.Text = aux.Length
Log ("Length=" & aux.Length & " Bytes")
Dim In As InputStream
In.InitializeFromBytesArray(aux, 0, aux.Length)
Dim Bitmap1 As Bitmap
Bitmap1.Initialize2(In)
In.Close
ImageView1.SetBackgroundImage(Bitmap1)

It hangs on: Bitmap1.Initialize2 (In)

These are the logs I get:
B4X:
/9j/4dorRXhpZgAASUkqAAgAAAAMAAABBAABAAA......

Message longer than Log limit (4000). Message was truncated.
Length=3300973 Bytes

main_astreams_newdata (java line: 379)

java.lang.OutOfMemoryError

    at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:652)
    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:724)
    at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.Initialize2(CanvasWrapper.java:519)
    at com.jr.tcpip1.main._astreams_newdata(main.java:379)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
    at anywheresoftware.b4a.BA$3.run(BA.java:312)
    at android.os.Handler.handleCallback(Handler.java:615)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4921)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
    at dalvik.system.NativeStart.main(Native Method)

Can anybody help me?

Thank you.
 

mc73

Well-Known Member
Licensed User
Longtime User
Erel is right of course.
Just a note: seeing the error in the initialization command, you should worry not for astreams, the problem is not on data size, but on the memory android os holds for each process and this brings occasional problems with large images. Loadbitmapsample is a solution.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…