Android Question java.lang.OutOfMemoryError into JSONGenerator

ebalvis

Member
Licensed User
Longtime User
I want to send a photo to a REST server

Code
B4X:
Sub sendPhoto(filename As String,claveSolicitud As Int,claveFoto As Int)
Dim lstData As List
Dim data As JSONGenerator
Dim out As OutputStream
    lstData.Initialize
      out.InitializeToBytesArray(100) 'size not really important
      File.Copy2(File.OpenInput(filename, ""), out)
    lstData.Initialize2(out.ToBytesArray)
    data.Initialize2(lstData)
    RESTcomm.ExecuteRPCRequestJson("PhotoServicio",Me,Array As String(claveSolicitud,claveFoto),data.ToString)
End Sub

In a Samsung SIII works perfectly but with any team lower, eg Samsung SII gives the following error
in line data.ToString
B4X:
java.lang.OutOfMemoryError


    at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:94)
    at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:145)
    at java.lang.StringBuilder.append(StringBuilder.java:216)
    at anywheresoftware.b4a.AbsObjectWrapper.toString(AbsObjectWrapper.java:111)
    at java.lang.String.valueOf(String.java:1681)
    at anywheresoftware.b4a.debug.Debug.objectToString(Debug.java:376)
    at anywheresoftware.b4a.debug.Debug.writeLocals(Debug.java:312)
    at anywheresoftware.b4a.debug.Debug.sendBPData(Debug.java:275)
    at anywheresoftware.b4a.debug.Debug.access$9(Debug.java:271)
    at anywheresoftware.b4a.debug.Debug$1.run(Debug.java:248)
    at android.os.Handler.handleCallback(Handler.java:605)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at anywheresoftware.b4a.Msgbox.waitForMessage(Msgbox.java:211)
    at anywheresoftware.b4a.Msgbox.debugWait(Msgbox.java:156)
    at anywheresoftware.b4a.debug.Debug.wait(Debug.java:205)
    at anywheresoftware.b4a.debug.Debug.reachBP(Debug.java:252)
    at anywheresoftware.b4a.debug.Debug.ShouldStop(Debug.java:224)
    at inn.grcplanta.main._sendphoto(main.java:1281)
    at inn.grcplanta.main._checknewphoto(main.java:506)
    at inn.grcplanta.main._rdp_onchangestate(main.java:1226)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
    at anywheresoftware.b4a.keywords.Common$4.run(Common.java:930)
    at android.os.Handler.handleCallback(Handler.java:605)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:4624)
    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:809)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
    at dalvik.system.NativeStart.main(Native Method)
 

ebalvis

Member
Licensed User
Longtime User
The server REST is implemented in Delphi XE3 only supports requests to methods using JSON objects.
What it could do is convert the image to Base64 string before sending.
 
Upvote 0

ebalvis

Member
Licensed User
Longtime User
Try to run your app in Release mode. The error message you get is related to the debugger.
Sorry for the delay in answering, but I needed another phone other than the SIII.
This is the error resulting in release mode
B4X:
threadid=1: thread exiting with uncaught exception (group=0x40018578)
main_sendphoto (B4A line: 126)
data.Initialize2(lstData)
java.lang.OutOfMemoryError
    at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:95)
    at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:153)
    at java.lang.StringBuilder.append(StringBuilder.java:217)
    at anywheresoftware.b4a.AbsObjectWrapper.toString(AbsObjectWrapper.java:111)
    at java.lang.String.valueOf(String.java:1860)
    at anywheresoftware.b4a.debug.Debug.objectToString(Debug.java:376)
    at anywheresoftware.b4a.debug.Debug.writeLocals(Debug.java:312)
    at anywheresoftware.b4a.debug.Debug.sendBPData(Debug.java:275)
    at anywheresoftware.b4a.debug.Debug.access$9(Debug.java:271)
    at anywheresoftware.b4a.debug.Debug$1.run(Debug.java:248)
    at android.os.Handler.handleCallback(Handler.java:587)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at anywheresoftware.b4a.Msgbox.waitForMessage(Msgbox.java:211)
    at anywheresoftware.b4a.Msgbox.debugWait(Msgbox.java:156)
    at anywheresoftware.b4a.debug.Debug.wait(Debug.java:205)
    at anywheresoftware.b4a.debug.Debug.reachBP(Debug.java:252)
    at anywheresoftware.b4a.debug.Debug.ShouldStop(Debug.java:224)
    at inn.grcplanta.main._sendphoto(main.java:1284)
    at inn.grcplanta.main._checknewphoto(main.java:509)
    at inn.grcplanta.main._rdp_onchangestate(main.java:1234)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
    at anywheresoftware.b4a.keywords.Common$4.run(Common.java:930)
    at android.os.Handler.handleCallback(Handler.java:587)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:130)
    at android.app.ActivityThread.main(ActivityThread.java:3687)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
    at dalvik.system.NativeStart.main(Native Method)
    at dalvik.system.NativeStart.main(Native Method)
 
Upvote 0

eps

Expert
Licensed User
Longtime User
java.lang.OutOfMemoryError

What size is the image? It needs to be downsampled (do a search here on OutOfMemoryError) and also look at increasing the heap size - which will help to a point.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…