3

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?

Rafał Sroka
  • 39,540
  • 23
  • 113
  • 143
litterbugkid
  • 3,534
  • 7
  • 36
  • 54

3 Answers3

4

Look at this example:

Uploading files to HTTP server using POST on Android.

Rafał Sroka
  • 39,540
  • 23
  • 113
  • 143
Maxim
  • 4,152
  • 8
  • 50
  • 77
  • Does that code split the file into parts or something? Although I don't think it's that kind of problem. I convert each file individually into a byte stream. I think the byte stream is too large hence the OutofMemory.. – litterbugkid Mar 16 '12 at 23:19
  • It gives OutputStream of the connection and reads the file into the stream. outputStream = new DataOutputStream( connection.getOutputStream(); Server needs to write that stream somewhere. If you read file by 1024 bytes it will never take more memmory then 1K. make a maxBufferSize = 4096. When you are opening file for streaming it doesn't get loaded into memmory – Maxim Mar 16 '12 at 23:24
  • If you show the piece of code that does upload, will be easier to guess what's wrong there. – Maxim Mar 16 '12 at 23:32
  • This does work for files bigger than 1MB, but now I am having trouble sending a file which is over 7MB in size. I get the OutOfMemory issue again.. – litterbugkid Mar 17 '12 at 15:10
3

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);
}
Community
  • 1
  • 1
litterbugkid
  • 3,534
  • 7
  • 36
  • 54
  • @Maxim Yes I fixed it. I upvoted your answer because it partly solved my problem. – litterbugkid Mar 19 '12 at 20:24
  • Thanks, but I was just curious did it work for you or not as I didn't have time to write a server part to find out why it is still didn't work. Scores don't matter – Maxim Mar 20 '12 at 14:40
0

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.

Lucifer
  • 29,392
  • 25
  • 90
  • 143