I'm trying to upload a large file ~10 MB on server and realized that on 2.3.4 the stream is written in memory first before writing to server, i confirm this behavior by looking into Heap Memory Dump, because of this for large file it causes OutOfMemory exception. I don't see the same behavior on 4.2 device.
Following is the code I'm using:
URL url = new URL(uri);
connection = (HttpsURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("PUT");
connection.setRequestProperty("content-type", "");
connection.setRequestProperty("Accept-Encoding", "");
connection.setFixedLengthStreamingMode((int)totalBytes);
out = new BufferedOutputStream(connection.getOutputStream());
fis = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[32 * 1024];
int bytesRead = 0;
int totalBytesRead = 0;
while ((bytesRead = fis.read(buffer)) != -1)
{
totalBytesRead = totalBytesRead + bytesRead;
out.write(buffer, 0, bytesRead);// OOM Error
}
out.flush();