Thursday, July 13, 2006

J2ME Image Loader Source Code

public Image getImageFromUrl(String url)
{
InputStream is = null;
HttpConnection hc = null;
Image img = null;
try {
hc = (HttpConnection)Connector.open(url);
// If we've got a connection?
if (hc.getResponseCode() == HttpConnection.HTTP_OK) {
// Open the stream
is = hc.openInputStream();
// How big is the file?
int len = (int)hc.getLength();
// Create a byte array that size
byte[] data = new byte[len];
// Read in the file
int actual = is.read(data);
// Create an image from the raw data
img = Image.createImage(data, 0, len);
}
} catch (Exception e) {
System.out.println("IO Exception+"+e);
} finally {
if (is != null) {
try {
is.close();
}
catch (Exception e) { }
}
if (c != null)
{
try {
c.close();
}
catch (Exception e) { }
}
return img;
}

1 comment:

banta said...

doesnt this throw an out of memory exception