在黑莓和显示下载图像

时间:2011-06-06 11:54:19

标签: blackberry httpconnection

我使用以下代码下载jpg图像并在黑莓曲线8900模拟器中显示。当我尝试加载我的代码时,它完全敲响并停止响应。请有人帮助我。

public DisplayBusinessDetail(String city,Business business)
{
    AbsoluteFieldManager absoluteFieldManager = new AbsoluteFieldManager();
    absoluteFieldManager.add(new BitmapField(connectServerForImage("http://www.mobileapples.com/Assets/Content/Wallpapers/Blackberry.jpg")),0,0);
    add(absoluteFieldManager);
}

public static Bitmap connectServerForImage(String url) {

      HttpConnection httpConnection = null;
      DataOutputStream httpDataOutput = null;
      InputStream httpInput = null;
      int rc;

      Bitmap bitmp = null;
      try {
       httpConnection = (HttpConnection) Connector.open(url);
       rc = httpConnection.getResponseCode();
       if (rc != HttpConnection.HTTP_OK) {
        throw new IOException("HTTP response code: " + rc);
       }
       httpInput = httpConnection.openInputStream();
       InputStream inp = httpInput;
       byte[] b = IOUtilities.streamToBytes(inp);
       EncodedImage hai = EncodedImage.createEncodedImage(b, 0, b.length);
       return hai.getBitmap();

      } catch (Exception ex) {
       System.out.println("URL Bitmap Error........" + ex.getMessage());
      } finally {
       try {
        if (httpInput != null)
         httpInput.close();
        if (httpDataOutput != null)
         httpDataOutput.close();
        if (httpConnection != null)
         httpConnection.close();
       } catch (Exception e) {
        e.printStackTrace();

       }
      }
      return bitmp;
     }

提前谢谢

1 个答案:

答案 0 :(得分:1)

您需要将套接字代码移出主UI事件线程。您的代码在UI线程上进行下载,这意味着在下载字节时没有UI响应。