【问题标题】:Blackberry: Download and display .jpg image黑莓:下载并显示 .jpg 图像
【发布时间】:2010-10-29 17:20:09
【问题描述】:

我正在尝试从服务器中提取 .jpg 图像,并将其显示为 EncodedImageZoomScreen 中。因为这个 .jpg 可能非常大,所以我想将 .jpg 保存到一个文件中并从那里读取它,所以我没有把整个东西放在内存中。

我面临的问题是,Connector.open("http.url.com/file.jpg") 要么抛出带有消息“Bad Socket Id”的IOException,要么抛出@987654324 @ 当我尝试打开一个FileConnection 到 URL 时。这是我尝试过的一个示例:

    try {
        FileConnection fileIn = (FileConnection)Connector.open(fileURL);
        FileConnection fileOut = (FileConnection)Connector.open(fileDest);

        if(!fileOut.exists())
            fileOut.create();

        InputStream is = fileIn.openInputStream();
        OutputStream os = fileOut.openOutputStream();

        while(fileIn.canRead() && fileOut.canWrite()){
            os.write(is.read());
        }

        is.close();
        os.close();

        fileIn.close();
        fileOut.close();

        EncodedImage image = EncodedImage.getEncodedImageResource(fileDest);

        UiApplication.getUiApplication().pushScreen(new ZoomScreen(image));

    } catch (Exception e) {
        e.printStackTrace();
    }

我从 RIM 获得了大部分信息,但我遗漏了一些东西。我知道 url 是正确的,因为当我从同一服务器流式传输音频时,我使用相同的格式。当我尝试连接到服务器时,第一行抛出异常。

有人有这方面的经验吗?

【问题讨论】:

    标签: blackberry download inputstream blackberry-simulator


    【解决方案1】:

    想通了。

    try {
        HttpConnection conn = (HttpConnection) Connector.open(fileURL);
        InputStream fileIn = conn.openInputStream();
    
        ByteArrayOutputStream os = new ByteArrayOutputStream();
    
        for(int i = fileIn.read(); i > -1; i = fileIn.read()){
            os.write(i);
        }
    
        byte[] data = os.toByteArray();
    
        EncodedImage image = EncodedImage.createEncodedImage(data, 0, data.length);
    
        UiApplication.getUiApplication().pushScreen(new ZoomScreen(image));
    
    } catch (Exception e) {
        e.printStackTrace();
    }
    

    我希望其他人会觉得这很有用。

    【讨论】:

      【解决方案2】:
      public void run(){
          try{
              StreamConnection streamC = (StreamConnection) Connector.open(url+";deviceside=true");
              InputStream in = streamC.openInputStream();
      
              byte[] data = new byte[4096];
              data = IOUtilities.streamToBytes(in);
              in.close();
                  EncodedImage eImage = EncodedImage.createEncodedImage(data, 0, data.length);
              }
          catch(Exception e)
              {
                  e.printStackTrace();
              }
      

      【讨论】:

      • 感谢您的额外输入,但您在这里所拥有的内容并不适用于所有内容。您只读取 4kB,但如果文件大于 4kB 怎么办?考虑到大多数 jpg 文件不是很大,您的文件适用于大多数情况,但并非适用于所有情况。
      猜你喜欢
      • 2011-09-09
      • 2011-09-23
      • 2012-06-23
      • 1970-01-01
      • 1970-01-01
      • 2013-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多