3

I'm using the Blackberry JDE (9000 simulator), and am wondering if I can display an image from the web.

Currently, I'm seeing tutorials that use Bitmap.getBitmapResource to display images that are local to the blackberry application, but looking at the API, I'm not seeing any support for giving a web URL.

Are there other Blackberry image classes I can check out? Or is this feature just not supported?

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
J.R.
  • 5,789
  • 11
  • 55
  • 78

3 Answers3

4

You can download image using HTTPConnection and InputStream, create EncodedImage from stream and then display it.

See coderholic - Blackberry WebBitmapField

BTW, you can use IOUtilities.streamToBytes() method to read bytes from InputStream directly!

Maksym Gontar
  • 22,765
  • 10
  • 78
  • 114
  • But, given that currenly all the ways to display something on a screen involve a "field" class...and there is only a bitmap field...does that mean to display the EncodedImage I'd have to write my own field? Or is there an alternative way to display an image? If so, what is it? – J.R. Sep 14 '09 at 16:09
  • If you read linked article, that is ok to setImage(EncodedImage) to BitmapField. But if you want, you can do graphics.drawImage() on paint() event in any class which extands Field, ex. in MainScreen, or HorizontalFieldManager. – Maksym Gontar Sep 14 '09 at 16:32
  • But its a new functionality to stream data for image from web, so I would suggest you to implement an extention to BitmapField, so you'll be able to use it many times. – Maksym Gontar Sep 14 '09 at 16:40
  • That tutorial seemed to do a lot of things. I did, however, get an image to display by looking at it. It seems weird to want to extend BitmapField, to me, when BitmapField already handles EncodedImages, and they don't seem that hard to generate. Thanks for the tips! – J.R. Sep 14 '09 at 17:31
1

Here is a code example for your problem:

    HttpConnection httpConn = null;
    InputStream inputStream = null;
    int ResponseCode = HttpConnection.HTTP_OK;
    byte[] ResponseData = null;

    try {
        httpConn = (HttpConnection) Connector.open(url, Connector.READ, true); 

        ResponseCode = httpConn.getResponseCode();
        if (ResponseCode == HttpConnection.HTTP_OK) {
            inputStream = httpConn.openInputStream();               
            ResponseData = IOUtilities.streamToBytes(inputStream);
        }
    }
    catch(IOException e){
        throw new IOException("HTTP response code: "
                + ResponseCode);
    }
    finally {
        try {
            inputStream.close();
            inputStream = null;
            httpConn.close();
            httpConn = null;
        }
        catch(Exception e){}
    }
    return ResponseData;
Lucifer
  • 29,392
  • 25
  • 90
  • 143
Hrvoje
  • 21
  • 1
0

If you want code that made to exactly do this (though this post is old, so I'm guessing you don't anymore)

Here

DFTR
  • 861
  • 10
  • 30