Reputation: 503
I am developing an application for uploading images from j2me mobile to server. As in j2me mobiles we are assigned only 1 mb of heap size, thus when I tried to upload image more than 1 mb , then it throw OutOfMemory exception. Is there any way to handle this issue in j2me mobiles.
Thanks.
Upvotes: 1
Views: 164
Reputation: 4043
It looks like you are trying to load the whole image to memory (using a byte[]
) before uploading it. If this is the case you should avoid doing it. You better use streams.
First get an InputStream
for the file and an OutputStream
for the server. Then copy all file contents from one to another using a buffer:
public static final void copy(InputStream from, OutputStream to)
throws IOException
{
byte [] buffer = new byte[1024];
int count = from.read(buffer);
while (count > 0) {
to.write(buffer, 0, count);
count = from.read(buffer);
}
to.flush();
}
Upvotes: 1