aspose file tools*
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
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "createImage from url" Watch "createImage from url" New topic
Author

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) 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

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: http://www.amazon.com/exec/obidos/ASIN/0137129394/mobileenterpr-20/
Ringful: http://www.ringful.com/
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
______________________________________
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

Joined: Apr 29, 2003
Posts: 28
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


------<br />www.beiks.net
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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) 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);
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: createImage from url