Upload files by post to server OutOfMemory

流过昼夜 提交于 2019-12-01 19:35:36

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

Add

conn.setFixedLengthStreamingMode(sourceFile.length());

below:

conn = (HttpURLConnection) url.openConnection();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!