简体   繁体   中英

Download image in blackberry and display

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM