|
J2ME HTTP GET Images
by Jason Lam
s
The code to read an image from the server is as follows:
DataInputStream iStrm = conn.openDataInputStream();
byte imageData[];
// If size is known
if (length != -1) {
imageData = new byte[length];
// Read the Image data into an array
iStrm.readFully(imageData);
// If size is NOT known
} else {
ByteArrayOutputStream bStrm = new ByteArrayOutputStream();
int ch;
while ((ch = iStrm.read()) != -1)
bStrm.write(ch);
imageData = bStrm.toByteArray();
bStrm.close();
}
// Contains Image retrieved in an Image Object
image = Image.createImage(imageData, 0, imageData.length);
|