coderholic

Blackberry WebBitmapField

The standard Blackberry API provides lots of useful UI components, known as fields. The BitmapField can be used to display an image. Recently I've needed to display images from the web, so I created the WebBitmapField class.

Anyone who's done any Blackberry programming knows that getting data off the web can be a bit of a pain. It must be done in a background thread, which must then pass the result back to the UI thread. I've created a simple method called getWebData that handles all of this for me. The result is passed back to a WebDataCallback interface:

public interface WebDataCallback
{
    public void callback(String data);
}

getWebData is a static method in my Utils class:

public static void getWebData(final String url, final WebDataCallback callback) throws IOException
{
    Thread t = new Thread(new Runnable()
    {
        public void run()
        {
            HttpConnection connection = null;
            InputStream inputStream = null;

            try
            {
                connection = (HttpConnection) Connector.open(url, Connector.READ, true);
                inputStream = connection.openInputStream();
                byte[] responseData = new byte[10000];
                int length = 0;
                StringBuffer rawResponse = new StringBuffer();
                while (-1 != (length = inputStream.read(responseData)))
                {
                    rawResponse.append(new String(responseData, 0, length));
                }
                int responseCode = connection.getResponseCode();
                if (responseCode != HttpConnection.HTTP_OK)
                {
                    throw new IOException("HTTP response code: "
                            + responseCode);
                }

                final String result = rawResponse.toString();
                UiApplication.getUiApplication().invokeLater(new Runnable()
                {
                    public void run()
                    {
                        callback.callback(result);
                    }
                });
            }
            catch (final Exception ex)
            {
                UiApplication.getUiApplication().invokeLater(new Runnable()
                {
                    public void run()
                    {
                        callback.callback("Exception (" + ex.getClass() + "): " + ex.getMessage());
                    }
                });
            }
            finally
            {
                try
                {
                    inputStream.close();
                    inputStream = null;
                    connection.close();
                    connection = null;
                }
                catch(Exception e){}
            }
        }
    });
    t.start();
}

The WebBitmapField that makes use of the getWebData method is below. All you need to do is pass a URL to the constructor and it'll load the image:

public class WebBitmapField extends BitmapField implements WebDataCallback
{
    private EncodedImage bitmap = null;

    public WebBitmapField(String url)
    {
        try
        {
            Util.getWebData(url, this);
        }
        catch (Exception e) {}
    }

    public Bitmap getBitmap()
    {
        if (bitmap == null) return null;
        return bitmap.getBitmap();
    }

    public void callback(final String data)
    {
        if (data.startsWith("Exception")) return;

        try
        {
            byte[] dataArray = data.getBytes();
            bitmap = EncodedImage.createEncodedImage(dataArray, 0,
                    dataArray.length);
            setImage(bitmap);
        }
        catch (final Exception e){}
    }
}

Hopefully the WebBitmapField class will be of use to some Blackberry developers. Feel free to use it in your applications.

Posted on 13 Mar 2008
If you enjoyed reading this post you might want to follow @coderholic on twitter or browse though the full blog archive.