Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

createImage from url

 
Diana Garc�a
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I'm trying to load an image from an url this way:
HttpConnection conn = (HttpConnection) Connector.open(url);
// 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
in.read(imageData);
// 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
author
Ranch Hand
Posts: 1427
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()".
 
Diana Garc�a
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
______________________________________
CODE:
HttpConnection conn = (HttpConnection) Connector.open(urlLogo);
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 = din.read(imageData, 0, 256);
if (rd == -1) break;
logo.append(imageData);
}

byte[] datos = (logo.toString()).getBytes();
imagen = Image.createImage(datos, 0, logo.length());
_______________________________________
 
Pavlin Mihalev
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
just two suggestions
- verify that the image is suported on the device, load it from a jar for example
- i have found that in.read(byte[]) doesn't work well for big arrays - try to read the data in a loop
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the problem might have something to do with your code appending byte arrays into a StringBuffer.
 
Diana Garc�a
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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) Connector.open(url);
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
iStrm.readFully(imageData);
}
else { // Length not available...
ByteArrayOutputStream bStrm = new ByteArrayOutputStream();
int ch;
while ((ch = iStrm.read()) != -1)
bStrm.write(ch);
imageData = bStrm.toByteArray();
bStrm.close();
}
// Create the image from the byte array
im = Image.createImage(imageData, 0, imageData.length);
}
finally {
// Clean up
if (iStrm != null)
iStrm.close();
if (connection != null)
connection.close();
}
return (im == null ? null : im);
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic