在BlackBerry上下载并显示图像

时间:2010-11-30 04:35:14

标签: blackberry

我必须开发一个网址,其中包括从网址下载图片并在黑莓刺激器中显示..可以在这方面帮助我吗???

2 个答案:

答案 0 :(得分:3)

此代码将连接给定的URL并返回Bitmap对象

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

你可以创建一个bimapfield并将这个位图设为

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

答案 1 :(得分:1)

对于基本64字符串解码

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