1

我的脚本通过 httpConnection 获取 xml 并保存到持久存储。那里没有问题。然后我遍历保存的数据以组成一个图像 url 列表以通过队列获取。

这些请求中的每一个都这样调用 httpConnection 线程

...

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

我找不到任何好的文档是如何获取响应数据并将其保存到存储在设备上的图像中。

也许我只是忽略了一些非常明显的事情。非常感谢任何帮助。谢谢


我尝试遵循此建议,并发现在查找 BB 特定问题时总是发现相同的东西:什么都没有。

问题是每个示例或帖子都假设您了解该平台的所有信息。这是一个简单的问题:哪一行代码将读取的输出流写入黑莓设备?什么路径?以后怎么找回?

我有这段代码,我不知道它是否有任何作用,因为我不知道它应该写到哪里,或者它是否正在做这件事:

** 文件名是根据调用的 url 在循环上确定的。

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();
    }
}

这个想法是我从服务器中提取了大约 600 张图像。我需要循环 xml 并将每个图像保存到设备中,以便在调用实体时,我可以从内部存储中提取关联的图像 - entity_id.png。

RIM 的文档没有具体说明这一点,也没有让开始弄清楚它变得容易。这个问题似乎没有在这个论坛上得到解决,或者我搜索过的其他人。

谢谢

4

1 回答 1

2

您需要使用 Java FileOutputStream 进行写入。从 InputStream 读取数据后,您还需要关闭连接(将 outputFinal 移到您的调用上方以关闭)。您可以轻松找到有关 FileOutputStream 的各种示例。

请参阅此处了解更多信息。请注意,为了使用 FileOutputStream,您的应用程序必须经过签名。

于 2010-04-23T21:24:06.523 回答