Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
SumiGhosh
Posts: 435
Registered: ‎09-20-2011
My Device: Z10 LE, Playbook, DevAlpha C, 9900, 9380

How to draw a bitmap whose source is an URL

Hi all,

  I know this is possible --------> Bitmap.getBitmapResource("icon.png");

  Is there any easy way where i can put the url as bitmap resource. On googling i'm only found  to get this through streaming, is that true.

Developer
RexDoug
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: How to draw a bitmap whose source is an URL

You would retrieve the bitmap data from the URL using an HTTP connection as a byte[], then rebuild the bitmap from the byte on this end.

 

Here is an example of "sample qualtity" code (not nec. ready for production but should give you a starting point):

 

http://www.coderholic.com/blackberry-webbitmapfield/

Developer
shaan_softwaredvlpr
Posts: 172
Registered: ‎01-17-2011
My Device: 9500 series

Re: How to draw a bitmap whose source is an URL

Developer
pankajace12
Posts: 206
Registered: ‎04-30-2011
My Device: BlackBerry Z10

Re: How to draw a bitmap whose source is an URL

HI

 

use this function...

 

Bitmap.getBitmapResource(downloadImage("your image source URL"));

 

/*******************************************************************************************************************/

 

 public static Bitmap downloadImage(String url)

{
InputStream iStream = null;
EncodedImage bitmap;
    HttpConnection httpConnection = null;
                                                               
        try
        {
                    httpConnection = (HttpConnection) Connector.open(url, Connector.READ_WRITE);
        
            httpConnection.setRequestMethod(HttpConnection.GET);
            int responseCode = httpConnection.getResponseCode();
            if (responseCode == HttpConnection.HTTP_OK) {
               
           
                iStream = httpConnection.openInputStream();
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
               
                byte[] buffer = new byte[256];
                int len = 0, imageSize = 0;
               
                while (-1 != (len = iStream.read(buffer))) {
                    byteArrayOutputStream.write(buffer);
                    imageSize += len;
                }
               
                byteArrayOutputStream.flush();
                byte[] imageData = byteArrayOutputStream.toByteArray();
                byteArrayOutputStream.close();
                bitmap = EncodedImage.createEncodedImage(imageData, 0, imageSize);                                      
                Bitmap bmp = bitmap.getBitmap();
 
returnbmp;
            }
            }
            catch (Exception e)
            {                      
            }
return null;                  
}