Ejemplo n.º 1
0
  public void run() {
    if (URL != null && URL.length() > 0) {
      ConnectionFactory f = new ConnectionFactory();
      ConnectionDescriptor descr = f.getConnection(URL);
      String targetURL;
      targetURL = descr.getUrl();
      HttpConnection httpConnection = null;
      DataOutputStream httpDataOutput = null;
      InputStream httpInput = null;
      int rc;

      try {
        httpConnection = (HttpConnection) Connector.open(targetURL);
        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);
        final EncodedImage hai = EncodedImage.createEncodedImage(b, 0, b.length);
        final Bitmap bmp = hai.getBitmap();
        System.out.println(bmp.toString());

        if (target != null) {
          UiApplication.getUiApplication()
              .invokeLater(
                  new Runnable() {

                    public void run() {
                      // TODO Auto-generated method stub
                      if (isDetailEvent == true) {
                        target.setBitmap(
                            DisplayHelper.CreateScaledCopyKeepAspectRatio(
                                bmp,
                                (int) (Display.getWidth() * 0.7),
                                (int) (Display.getHeight() * 0.3)));
                      } else {
                        target.setBitmap(
                            DisplayHelper.CreateScaledCopyKeepAspectRatio(
                                bmp, target.getBitmapWidth(), target.getBitmapHeight()));
                      }
                    }
                  });
        }

        if (localPath != null) {
          Utils.saveBitmap(localPath, bmp);
        }

        if (imageCache != null) {
          if (Utils.saveBitmap(
              ImageCacheModel.getImageCacheDirectory() + imageCache.getFileName(), bmp)) {
            //		    		   CacheUtils.getInstance().addImageCache(imageCache);
            System.out.println("image write success");
          }
        }

        if (callback != null) callback.onImageDownloaded(true, bmp);

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