Sending file > 1MB using HTTP POST

2019-02-19 13:55发布

问题:

I'm sending video and audio files from my Android application to Wampserver, some of these can get quite large and I tend to get OutofMemory issues when the file is approximately over 1MB in size.

I convert each file individually into a byte stream. I think the byte stream is too large hence the OutofMemory.

How can I stop this error from occurring?

回答1:

Using the link Maxium suggested here:

Uploading files to HTTP server using POST on Android.

I then found this Out of Memory error in android to fix the error.

Replace:

while (bytesRead > 0)
{
    outputStream.write(buffer, 0, bufferSize);
    bytesAvailable = fileInputStream.available();
    bufferSize = Math.min(bytesAvailable, maxBufferSize);
    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}

with:

while (bytesRead > 0){
    bufferSize = Math.min(bytesAvailable, maxBufferSize);
    byte byt[]=new byte[bufferSize];
    fileInputStream.read(byt);
    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
    outputStream.write(buffer, 0, bufferSize);
}


回答2:

Look at this example:

Uploading files to HTTP server using POST on Android.



回答3:

When you have large size of data like big file or big image/good quality image to send to server then you can send it in parts/chunks.

First make Http Connection with the Server and then send chunks with DataOutputStream Class. On the server side same, you need to implement code for receiving these chunks and make them in one file once you get all the chunks.