Upload files by post to server OutOfMemory

前端 未结 2 1787
你的背包
你的背包 2021-01-19 05:35

I\'m developing a remote backup app, and Sometimes I need upload big files as for example 15 MB, I have tested in some phones I get an out of memory error

Is there a

2条回答
  •  再見小時候
    2021-01-19 06:15

    You should use either the setChunkedStreamingMode() or setFixedLengthStreamingMode() methods of the HttpURLConnection. This will prevent the buffering of your data in memory and exhausting it.

    Relevant quote from the documentation:

    For best performance, you should call either setFixedLengthStreamingMode(int) when the body length is known in advance, or setChunkedStreamingMode(int) when it is not. Otherwise HttpURLConnection will be forced to buffer the complete request body in memory before it is transmitted, wasting (and possibly exhausting) heap and increasing latency.

    More here: http://developer.android.com/reference/java/net/HttpURLConnection.html

提交回复
热议问题