0

I'm trying to upload a large file using HttpsURLConnection (together with other tokens), if I'm uploading it in one chunk, obviously, I'm getting an OutOfMemory exception.
I tried to follow this example: http://www.cuelogic.com/blog/android-code-to-upload-download-large-files-to-server-2/
But now I'm getting a "I/O error during system call, Broken pipe" error.

I saw that this a known issue, but I didn't find any good solution. How can I solve this?

Sharas
  • 1,985
  • 3
  • 20
  • 43
  • Please search for answers before posting question here! Refer this http://stackoverflow.com/questions/2017414/post-multipart-request-with-android-sdk – Rohit Sep 30 '14 at 07:05
  • Thanks, but i did search and didn't find the answer. please note that my problem is with sending large files, and not with creating multi part request – Sharas Sep 30 '14 at 10:42

1 Answers1

0

Try this way

public void connectForMultipart() throws Exception {
    con = (HttpURLConnection) ( new URL(url)).openConnection();
    con.setRequestMethod("POST");
    con.setDoInput(true);
    con.setDoOutput(true);
    con.setRequestProperty("Connection", "Keep-Alive");
    con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
    con.connect();
    os = con.getOutputStream();
}

public void addFormPart(String paramName, String value) throws Exception {
    writeParamData(paramName, value);
}

public void addFilePart(String paramName, String fileName, byte[] data) throws Exception {
    os.write( (delimiter + boundary + "\r\n").getBytes());
    os.write( ("Content-Disposition: form-data; name=\"" + paramName +  "\"; filename=\"" + fileName + "\"\r\n"  ).getBytes());
    os.write( ("Content-Type: application/octet-stream\r\n"  ).getBytes());
    os.write( ("Content-Transfer-Encoding: binary\r\n"  ).getBytes());
    os.write("\r\n".getBytes());

    os.write(data);

    os.write("\r\n".getBytes());
}   
public void finishMultipart() throws Exception {
    os.write( (delimiter + boundary + delimiter + "\r\n").getBytes());
}

you can request to use more memory by using

 android:largeHeap="true"

in the manifest.

also, you can use native memory (NDK & JNI) , so you actually bypass the heap size limitation.

here are some posts made about it:

and here's a library made for it:

happy coding

regards maven

Community
  • 1
  • 1
Maveňツ
  • 1
  • 12
  • 50
  • 89
  • Thanks, but my problem is with large files, this solution won't solve this... as it uploads the file in one chunk... – Sharas Sep 30 '14 at 10:41