Posting a large file in Android

后端 未结 2 874
温柔的废话
温柔的废话 2020-12-21 15:59

I sometimes get an OutOfMemoryError when posting a large file in Android. This is the code I\'m using. Am I doing something wrong?

HttpURLConne         


        
相关标签:
2条回答
  • 2020-12-21 16:46

    HTTP connection is fine, but HTTPS will trigger Out of Memory error because there is a bug in HttpsURLConnectionImpl.java in Android 2.3.4 (verified on my tablet), and it's fixed in Android 4.1 (I have checked the source code).

    By the way, he should avoid buffering the whole POST data in RAM by adding "con.setChunkedStreamingMode(0);" immediately after "con.setDoOutput(true);" statement.

    0 讨论(0)
  • 2020-12-21 16:56

    If you do it like that, the whole POST must be buffered in memory. This is because it needs to send the Content-Length header first.

    Instead, I think you want to use the Apache HTTP libraries, including FileEntity. That will let it figure out the length before reading the file. You can use this answer as a starting point. But the second parameter to the FileEntity constructor should be a mime type (like image/png, text/html, etc.).

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