Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is it possible to upload large files with Volley? ( Android )

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?

like image 505
Iter Ator Avatar asked Nov 13 '20 18:11

Iter Ator


People also ask

How do I handle a large upload?

Possible solutions: 1) Configure maximum upload file size and memory limits for your server. 2) Upload large files in chunks. 3) Apply resumable file uploads. Chunking is the most commonly used method to avoid errors and increase speed.


2 Answers

Volley has some known issues with large file uplaod.

Use multipart file upload for large files, for instance (with retrofit):

public interface FileUploadService {

    @Multipart
    @POST("/upload")
    void upload(@Part("myfile") TypedFile file,
                @Part("description") String description,
                Callback<String> cb);
}

For your reference : https://futurestud.io/blog/retrofit-how-to-upload-files/

like image 191
TanvirChowdhury Avatar answered Sep 25 '22 08:09

TanvirChowdhury


Read this: Volley Issue

From the link: "It[volley] inherently requires that the full request/response be storable in memory, and for video uploads it makes far more sense to stream them incrementally over the network from another source (i.e. disk), to support resume of partial uploads, etc, in a way that Volley's API inherently doesn't support."

like image 24
Sina Avatar answered Sep 21 '22 08:09

Sina