I would like to upload large files in a POST request using Volley. I tried to use a VolleyMultiPartRequest library, but I get java.lang.OutOfMemoryError
:
2020-11-13 19:14:06.636 18802-18802/com.example.myproject E/MainActivity: Tried to start foreground service from background
2020-11-13 19:14:09.730 18802-19082/com.example.myproject E/AndroidRuntime: FATAL EXCEPTION: Thread-15
Process: com.example.myproject, PID: 18802
java.lang.OutOfMemoryError: Failed to allocate a 303955984 byte allocation with 8388608 free bytes and 108MB until OOM, max allowed footprint 162626224, growth limit 268435456
at java.util.Arrays.copyOf(Arrays.java:3164)
at java.io.ByteArrayOutputStream.grow(ByteArrayOutputStream.java:118)
at java.io.ByteArrayOutputStream.ensureCapacity(ByteArrayOutputStream.java:93)
at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:153)
at com.example.myproject.utils.VolleyMultiPartRequest2.getBody(VolleyMultiPartRequest2.java:109)
at com.android.volley.toolbox.HurlStack.addBodyIfExists(HurlStack.java:275)
at com.android.volley.toolbox.HurlStack.setConnectionParametersForRequest(HurlStack.java:249)
at com.android.volley.toolbox.HurlStack.executeRequest(HurlStack.java:94)
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:123)
at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:131)
at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:111)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:90)
All of the libraries like VolleyPlus MultiPartRequest overrides the function public byte[] getBody()
. This seems to be the problem, because if a large file is converted into a byte array, then it can not fit into the memory. I need to send the request in chunks. How is that possible?