views:

325

answers:

2

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

+2  A: 

You'll need to use the Java FileOutputStream to do the writing. You'll also want to close the connection after reading the data from the InputStream (move outputFinal above your call to close). You can find all kinds of examples regarding FileOutputStream easily.

See here for more. Note that in order to use the FileOutputStream your application must be signed.

SB
A: 

I tried following this advise and found the same thing I always find when looking up BB specific issues: nothing.

The problem is that every example or post assumes you know everything about the platform. Here's a simple question: What line of code writes the read output stream to the blackberry device? What path? How do I retrieve it later?

I have this code, which I do not know if it does anything because I don't know where it is supposedly writing to or if that's even what it is doing at all:

** filename is determined on a loop based on the url called.

FileOutputStream fos = null;
try
{
    fos = new FileOutputStream( File.FILESYSTEM_PATRIOT, filename );

    byte [] buffer = new byte [262144];
    int byteRead;
    while ((byteRead = result.read (buffer ))!=- 1)
    {
        fos.write (buffer, 0, byteRead);
    }

    fos.flush();
    fos.close();   
}
catch(IOException ieo)
{ 
}
finally
{
    if(fos != null)
    {
        fos.close();
    }
}

The idea is that I have some 600 images pulled from a server. I need to loop the xml and save each image to the device so that when an entity is called, I can pull the associated image - entity_id.png - from the internal storage.

The documentation from RIM does not specify this, nor does it make it easy to begin figuring it out. This issue does not seem to be addressed on this forum, or others I have searched.

Thanks

Kai
filename is something like: file:///store/home/user/folder/300.jpgno ideas? good... good
Kai
That's a file URL, and yes it would appear to be fine, but your application needs to be signed in order to save things in J2ME land.
SB