How do you save images to a Blackberry device via HttpConnection?

Posted by Kai on Stack Overflow See other posts from Stack Overflow or by Kai
Published on 2010-04-23T21:13:09Z Indexed on 2010/04/26 23:23 UTC
Read the original article Hit count: 404

Filed under:
|
|

My script fetches xml via httpConnection and saves to persistent store. No problems there. Then I loop through the saved data to compose a list of image url's to fetch via queue.

Each of these requests calls the httpConnection thread as so

...

public synchronized void run()
 { 
         HttpConnection connection = (HttpConnection)Connector.open("http://www.somedomain.com/image1.jpg");
         connection.setRequestMethod("GET");
         String contentType = connection.getHeaderField("Content-type");

         InputStream responseData = connection.openInputStream();
         connection.close();

         outputFinal(responseData, contentType);
}

public synchronized void outputFinal(InputStream result, String contentType) throws SAXException, ParserConfigurationException, IOException
 {

  if(contentType.startsWith("text/"))
  {
       // bunch of xml save code that works fine

  }
  else if(contentType.equals("image/png") || contentType.equals("image/jpeg") || contentType.equals("image/gif"))
  {
   // how to save images here?
  }
  else
  {
   //default
  }
 }

What I can't find any good documentation on is how one would take the response data and save it to an image stored on the device.

Maybe I just overlooked something very obvious. Any help is very appreciated. Thanks

© Stack Overflow or respective owner

Related posts about blackberry

Related posts about httpconnection