简体   繁体   中英

Download and show image on a BlackBerry

我必须开发一个URL,其中涉及从URL下载图像并显示在Blackberry刺激器中。在这方面有人可以帮助我吗?

This code 'll connect the given URL and returns Bitmap object

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

U can create a bimapfield and asign this bitmap as

BitmapField bmpFld1=new BitmapField(connectServerForImage(Url));

For base 64 string decoding

 try {
    mapaByte = Base64InputStream.decode(imagenB64);
    Bitmap mapa64 = Bitmap.createBitmapFromBytes(mapaByte, 0, -1, 1);
    mapa.setBitmap(mapa64);
     } 
 catch (Exception e) {}

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