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

3

10

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?

Benzofuran answered 13/11, 2020 at 18:22 Comment(2)
Last time I used volley I didn't find any solution for this problem , Had to use some other library just for uploading .Contexture
Agreed: stop using Volley. As we discussed previously, OkHttp does not read the entire content to upload into memory, but rather works using streams.Seidule
C
2

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."

Cathay answered 18/11, 2020 at 6:45 Comment(0)
S
3

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/

Saloop answered 19/11, 2020 at 10:49 Comment(0)
C
2

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."

Cathay answered 18/11, 2020 at 6:45 Comment(0)
B
2

As @CommonsWare mentioned in the comment below the question - you should drop Volley(at least for now). Some time ago they even wanted to deprecate Volley because it depends on Apache HttpClient which is deprecated since API 23, but as far as I know google still supports it and has intentions to make it more modern.

For now stick to the Retrofit and/or underlying OkHttp which leverage stream upload/download rather than full load to memory. With those you won't be having such issues.

Bunkum answered 19/11, 2020 at 10:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.