Android Question Httpjob - How to trap Out of Memory error

Neojoy

Member
Licensed User
Longtime User
Hi,

How can I trap "out of memory" error on httpjob before my app crashes when I try to upload large files, try and catch not works.

Is possible to check avaliable memory with file size to know if my app can upload or not?
 

DonManfred

Expert
Licensed User
Longtime User
How are you creating the request? How are you sending the upload? How big is the file you want to upload?
 
Upvote 0

Neojoy

Member
Licensed User
Longtime User
Just started writing similar things. If you do it correctly you can send files of any size.

I'm using code below

B4X:
Dim job1 As HttpJob 'Process_Globals

job1.Initialize("job1", Me) 'Service_Create


Dim fd As MultipartFileData
fd.Initialize
fd.KeyName = "file1"
fd.Dir = "mydir"
fd.FileName = "myfile.ext"
fd.ContentType = "ImageType"
job1.GetRequest.Timeout=60000
job1.PostMultipart("http://mysever/mypage.php", CreateMap("filename": filename), Array(fd))

when the file is too big (100 MB) cause de follow error:

B4X:
httpjob_vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv7 (java line: 499)
java.lang.OutOfMemoryError: Failed to allocate a 77553676 byte allocation with 16765216 free bytes and 38MB until OOM
    at java.util.Arrays.copyOf(Arrays.java:3256)
    at java.io.ByteArrayOutputStream.grow(ByteArrayOutputStream.java:113)
    at java.io.ByteArrayOutputStream.ensureCapacity(ByteArrayOutputStream.java:93)
    at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:140)
    at anywheresoftware.b4a.objects.streams.File.Copy2(File.java:362)
    at com.teste.httpjob._vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv7(httpjob.java:499)
    at com.teste._vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv2(notificationservice.java:12399)
    at com.teste._chknewimg(notificationservice.java:1264)
    at com.teste._jobdone(notificationservice.java:4927)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:191)
    at anywheresoftware.b4a.keywords.Common$11.run(Common.java:1154)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6312)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
 
Upvote 0
Top