Friday, April 27, 2012

Download image from server in BlackBerry

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

No comments:

Post a Comment