File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java Micro Edition and the fly likes createImage from url Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "createImage from url" Watch "createImage from url" New topic

createImage from url

Diana Garc�a
Ranch Hand

Joined: Sep 21, 2003
Posts: 31
Hello, I'm trying to load an image from an url this way:
HttpConnection conn = (HttpConnection);
// Check to see if we successfully opened the connection
int response = conn.getResponseCode();
if(response == HttpConnection.HTTP_OK ) {
int longit = (int) conn.getLength();
if (longit > 0) {
byte imageData[] = new byte[longit];
InputStream in = conn.openInputStream();
// Read the png into an array;
// Create the image from the byte array
imagen = Image.createImage(imageData, 0, longit-1);
Something's wrong but I don't know what: I receive an Exception when I create the image. Can anybody help?
Thanks in advantadge
Michael Yuan
Ranch Hand

Joined: Mar 07, 2002
Posts: 1427
Check out this thread. Basically, you cannot trust the "content-length" header. Even if you can trust it, "readFully()" probably works better than a simple "read()".

Seam Framework:
Diana Garc�a
Ranch Hand

Joined: Sep 21, 2003
Posts: 31
Thanks a lot for answering. I've checked the thread you suggested and think I've understood the problem. According to what there is explained, I've changed my code but it still doesn't work. Would you please take a look?
Thanks again
HttpConnection conn = (HttpConnection);
int response = conn.getResponseCode();
if(response != HttpConnection.HTTP_OK) return null;

DataInputStream din = conn.openDataInputStream();
byte[] imageData = new byte[256];
StringBuffer logo = new StringBuffer();
while (true) {
int rd =, 0, 256);
if (rd == -1) break;

byte[] datos = (logo.toString()).getBytes();
imagen = Image.createImage(datos, 0, logo.length());
Pavlin Mihalev

Joined: Apr 29, 2003
Posts: 28
just two suggestions
- verify that the image is suported on the device, load it from a jar for example
- i have found that[]) doesn't work well for big arrays - try to read the data in a loop

------<br />
Lasse Koskela

Joined: Jan 23, 2002
Posts: 11962
I think the problem might have something to do with your code appending byte arrays into a StringBuffer.

Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Diana Garc�a
Ranch Hand

Joined: Sep 21, 2003
Posts: 31
Everything is right now. If anyone needs help, this works:
Thanks a lot for your help!!
private Image getImage(String url) throws IOException {
HttpConnection connection = (HttpConnection);
DataInputStream iStrm = connection.openDataInputStream();

Image im = null;
try {
// ContentConnection includes a length method
byte imageData[];
int longit = (int) connection.getLength();
if (longit != -1) {
imageData = new byte[longit];
// Read the png into an array
else { // Length not available...
ByteArrayOutputStream bStrm = new ByteArrayOutputStream();
int ch;
while ((ch = != -1)
imageData = bStrm.toByteArray();
// Create the image from the byte array
im = Image.createImage(imageData, 0, imageData.length);
finally {
// Clean up
if (iStrm != null)
if (connection != null)
return (im == null ? null : im);
I agree. Here's the link:
subject: createImage from url
It's not a secret anymore!