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
dinu_hasith
Posts: 15
Registered: ‎10-23-2008
My Device: Not Specified
Accepted Solution

how to get animated gif file using Http connection?

Hi,

    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);
httpConnection.setRequestMethod(HttpConnection.GET);

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

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

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

data.length);

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

gifImage = null;
e.printStackTrace();
}

 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.

 

Dinu 

 

 

 


 

 

Please use plain text.
Developer
dinu_hasith
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. :mantongue:

 

 

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

 

 

 

Please use plain text.
New Developer
aksudhakar2002
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);
            response.setContentType("image/JPEG");
            //ImageIO.write(bufi,"JPEG",response.getOutputStream());

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

 

 

ClientSide (BB)

------------------------

 httpConnection = (HttpConnection) Connector.open(url, Connector.READ_WRITE);
            httpConnection.setRequestMethod(HttpConnection.GET);
                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();

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

 

 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
Please use plain text.
Developer
dinu_hasith
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
Please use plain text.