Upload files by post to server OutOfMemory

前端 未结 2 1788
你的背包
你的背包 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:09

    Add

    conn.setFixedLengthStreamingMode(sourceFile.length());
    

    below:

    conn = (HttpURLConnection) url.openConnection();
    
    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题