开发者

Download image in blackberry and display

开发者 https://www.devze.com 2023-03-10 22:34 出处:网络
I use following code to download jpg image and display in blackberry curve 8900 simulator. When I am trying to load my code, it strucks completely and stop responding. Please some one help me.

I use following code to download jpg image and display in blackberry curve 8900 simulator. When I am trying to load my code, it strucks completely and stop responding. Please some one help me.

public DisplayBusinessDetail(String 开发者_如何学Ccity,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;
     }

Thank you in advance


You need to move the socket code off of the main UI event thread. Your code does the download on the UI thread, meaning no UI responsiveness while downloading the bytes.

0

精彩评论

暂无评论...
验证码 换一张
取 消