1

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();

1 Answer 1

1

I filed a bug with google-android and they claim it's fixed for GingerBread release but I'm still able to replicate the issue in 2.3.4

Link to the bug https://code.google.com/p/android/issues/detail?id=53946 and https://code.google.com/p/android/issues/detail?id=3164

I ended up using HttpClient for Eclair, Froyo, GingerBread and HttpURLConnection for HoneyComb and above

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.