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

Posts: 15
Registered: ‎10-23-2008
My Device: Not Specified
Accepted Solution

how to get animated gif file using Http connection?


    I have an application that downloads gif animations from a server using http connection. here's the code that i use to download the image.


try {

httpConnection = (HttpConnection) Connector.open(url, Connector.READ_WRITE);

if (httpConnection.getResponseCode() == HttpConnection.HTTP_OK) {
DataInputStream dS = httpConnection.openDataInputStream();

byte[] data = new byte[dS.available()];

gifImage = (GIFEncodedImage) EncodedImage.createEncodedImage(data, 0,


} else {
throw new Exception("ResponseCode:" + httpConnection.getResponseCode());
} catch (Exception e) {

gifImage = null;

 the problem with this code is, when the gif file is larger than 1175 bytes, it doesnt load the gif.


example:- if the gif on the server is 1500 bytes, this code only retrieves 1175 bytes.


I think the rest of the bytes are sent in a different packet. but how to get them.

can anyone please provide a code to buffer the inputstream?


all help appreciated.








Posts: 15
Registered: ‎10-23-2008
My Device: Not Specified

Re: how to get animated gif file using Http connection?

hmm, nobody seems to knw how to achieve this it seems. here is how I did it. Man Tongue



httpConnection = (HttpConnection) Connector.open(url, Connector.READ_WRITE); httpConnection.setRequestMethod(HttpConnection.GET); int responseCode = httpConnection.getResponseCode(); if (responseCode == HttpConnection.HTTP_OK) { inputStream = httpConnection.openInputStream(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[256]; int len = 0, imageSize = 0; while (-1 != (len = inputStream.read(buffer))) { byteArrayOutputStream.write(buffer); imageSize += len; } byteArrayOutputStream.flush(); byte[] imageData = byteArrayOutputStream.toByteArray(); byteArrayOutputStream.close(); gifImage = (GIFEncodedImage) EncodedImage.createEncodedImage(imageData, 0, imageSize);




New Developer
Posts: 24
Registered: ‎05-14-2009
My Device: Not Specified

Re: how to get animated gif file using Http connection?

[ Edited ]

hi ,

        i tried your code but I am getting illegalArgument Exception.. I am not using Animated image.. 


ServerSide Code


        File myGifFile = new File("E:/jenny.jpg");
            //BufferedImage bufi = ImageIO.read(myGifFile);

            OutputStream os = response.getOutputStream();
            byte[] data = readFile(myGifFile);
            os.write(data, 0, data.length); // sending image to blackberry
          /*  response.setContentType("text/HTML");
            PrintWriter out1 =  response.getWriter();
            out1.flush(); */



ClientSide (BB)


 httpConnection = (HttpConnection) Connector.open(url, Connector.READ_WRITE);
                inputStream = httpConnection.openInputStream();
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

                byte[] buffer = new byte[256];
                int len = 0, imageSize = 0;

                while (-1 != (len = inputStream.read(buffer))) {
                    imageSize += len;
                byte[] imageData = byteArrayOutputStream.toByteArray();

                GIFEncodedImage gifImage = (GIFEncodedImage) EncodedImage.createEncodedImage(imageData, 0, imageSize);
                Bitmap bm = gifImage.getBitmap();
                BitmapField wimblogoBmf = new BitmapField(bm);


 When Debug control reaches the line marked in red.. i am getting the   EXCEPTION:java.lang.IllegalArgumentException.

In server side I am printing the bytearray by placing the content type(text/HTML). i have also tried by placing the content type ( image/jpeg) .


Can u please help where i have gone wrong..

Message Edited by aksudhakar2002 on 09-01-2009 07:24 PM
Posts: 15
Registered: ‎10-23-2008
My Device: Not Specified

Re: how to get animated gif file using Http connection?

[ Edited ]

First try to download a gif to see whether the code works. you'll have to set the content-type to image/gif.


if u can download the gif successfully then try,


since you're downloading a jpeg, you'll hve to use Bitmap or Image class to create the jpeg instead of using GIFEncodedImage class.


try it out...

Message Edited by dinu_hasith on 09-02-2009 11:01 AM