OutOfMemoryError When Sending Large Image
Hi All,
We are developing a midlet that is esentially a photo blogging app. We are using an HTTP POST to send the image to the web server. The code is working properly as we are able to send images up to about 80KB.
However, when sending images larger than ~80KB, the midlet gets an OutOfMemoryError.
The image is being sent in chunked data packets, so shouldn't this mean we could send any size file since it would just keep sending more data chunks until it has reached the end of the file?...
Has anyone else out there encountered this or perhaps know of a work around?
Any help would be greatly appreciated.
Thanks!
Jim
[674 byte] By [
JimDVa] at [2007-11-27 8:13:31]

# 3
We are currently loading the entire image into memory at the moment which is
probably causing the OutOfMemory exceptions. Would you know how native
phone applications send images through HTTP which are many times the
size of available memory?
The only way I could think of is to somehow connect the input stream
which gets the image from the phone's memory and the output stream
which writes out the HTTP data. By doing this no new byte array will get
declared (explicitly anyway) to temporarily hold the entire image in the phone's
memory. I'm wondering could I accomplish something like that by somehow
collapsing these two chunks of code into one that declares no arrays to hold
the entire image in memory?
Here's our current code for reference:
Getting the image from the phone
-
theFile is a FileConnection object which references the image file we want
InputStream fileInputStream = theFile.openInputStream();
fileContent = new byte[(int)filesize];
fileInputStream.read(fileContent);
fileInputStream.close();
Writing image to HTTP output stream
--
data is a byte[] array holding the entire image in memory
httpOut is a DataOutputStream
SuperViewerMidlet.httpOut.write(data);
JimDVa at 2007-7-12 19:57:56 >

# 4
Come on, this is so obvious. Just read the image bit by bit and transfer the data to your http stream
InputStream fileInputStream = theFile.openInputStream();
byte buffer = new byte[8192];
int rd;
while((rd = fileInputStream.read(buffer ))!=-1){
SuperViewerMidlet.httpOut.write(buffer,0,rd);
}
fileInputStream.close();