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
Regular Contributor
Posts: 92
Registered: ‎01-31-2012
My Device: curve 8520
My Carrier: vodafone

Encoded Image error

I am developing a blackberry application which retrieves image from the server , some of images are being retreived and other give error in the encodedImage Line

    

   ImageFromUrl _img = new ImageFromUrl(item.getThumbLink());
                byte[] bytes = _img.getbitmap();
                Bitmap newBitmap = new Bitmap(width, fieldHeight);
                if (bytes != null) {
                    // bitmap = Bitmap.createBitmapFromBytes(bytes, 0,
                    // bytes.length, Bitmap.SCALE_TO_FIT);
                    EncodedImage image = EncodedImage.createEncodedImage(bytes,
                            0, bytes.length);

 



and that's the connection which get the bytes

 

  ImageFromUrl(String url) {
        this.url = url;

    }

    public byte[] getbitmap() {

        // TODO Auto-generated method stub
        try {

            connection = (HttpConnection) Connector.open(
                    url + Connection.getBlackBerryConnectionParams(),
                    Connector.READ, true);

            InputStream is = connection.openInputStream();

            DataInputStream dis = new DataInputStream(is);
            ByteArrayOutputStream bStrm = new ByteArrayOutputStream();

            int ch;
            while ((ch = dis.read()) != -1) {
                // System.out.println((char) ch);
                // msg = msg + (char) ch;
                bStrm.write(ch);
            }
            dataArray = bStrm.toByteArray();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            
        }

        // TODO Auto-generated method stub

        return dataArray;

    }

 



how can I solve that to get all images without errors ?

Developer
Posts: 19,621
Registered: ‎07-14-2008
My Device: Not Specified

Re: Encoded Image error

Can you put a try catch around the processing to determine exactly what error you are seeing when it fails?

 

It looks like you are doing this processing on the Event Thread, e.g. as part of the creation of your screen.  To test  if this is the case, put code like this in your getbitmap() method.

 

if (Application.isEventDispatchThread() ) {

Dialog.alert("this processing will fail on a real device");

}

 

If you see the Dialog, you are running the download on the Event Thread.  This will fail on a real device.  Search the forum for other Threads that discuss downloading and Event Thread, including

http://supportforums.blackberry.com/t5/Java-Development/What-is-the-Event-Thread/ta-p/446865

Regular Contributor
Posts: 92
Registered: ‎01-31-2012
My Device: curve 8520
My Carrier: vodafone

Re: Encoded Image error

download doesn't use the event thread and I get some messages from the server but other images can't be retrieved and encoded image error in the debug  ,check this snapshot Capture.PNG

the two items which I add the blue in their place can't be retreived and this problem in some other cases 

sry for the late replying

Developer
Posts: 19,621
Registered: ‎07-14-2008
My Device: Not Specified

Re: Encoded Image error

Sorry about asking you to check the Event Thread.

 

I must admit to being a little perplexed as to the problems you are having.

 

Unfortunately, the next thing I would do is quite hard, that is, if the byte array you are being supplied does not encode properly, you have to compare the bytes you received with what is actually on the host.  This means dumping out the length and the data on the Blackberry and using a hex editor to physically look at the bytes.  You will probably find on the Blackberry you only have to dump out the first and last few bytes. 

 

The above assumes that you do not consistently get the problem wit the same image.   If you always have a problem with a specific image, then I would try to replicate the download convert processing in a test program.  Include the failing image in a test program as a resource, read it as a byte array, and then encode the byte array (in other words do not use Bitmap processing). 

 

I am interested to see what happens in either case. 

 

Sorry that I can't be more helpful. 

Regular Contributor
Posts: 92
Registered: ‎01-31-2012
My Device: curve 8520
My Carrier: vodafone

Re: Encoded Image error

it's very difficult :smileyfrustrated: , is there any other method for encoding images ? ,btw images on the server are working and it's in jpg format
Developer
Posts: 19,621
Registered: ‎07-14-2008
My Device: Not Specified

Re: Encoded Image error

Look at the EncodedImage class in the JavaDocs for the level that you are targeting to see what options there are.  For example in OS 5/0, there is B<P, GIF, JPEG, PNG, TIFF and WBMP. 

 

I am still not sure which if the two possible issues you have:

a) The data is not being downloaded correctly

b) The file that is supplied is not encoded in a way that the BlackBerry understands.

 

If it was (b0, then you should find an image that consistently does not work in a device (perhaps not every device,  perhaps there is additional encoding support added in later OSes so the file becomes supported in specific OSes). 

 

If it was (a), the the same file would sometimes work and sometimes not work in the same device. 

 

have you enough evidence to choose either (a) or (b)?

Regular Contributor
Posts: 92
Registered: ‎01-31-2012
My Device: curve 8520
My Carrier: vodafone

Re: Encoded Image error

data is downloading correctly as some photos are shown as you see and I tried another way of downloading it didn't affect any thing but it increased the photos which not shown , I think the file on the server but what's the proper encoding of blackberry ? , how should I change it ?!!
Developer
Posts: 19,621
Registered: ‎07-14-2008
My Device: Not Specified

Re: Encoded Image error

"data is downloading correctly as some photos are shown as you see"

I would like to keep an open mind about this, but I agree this is less likely. 

 

If you have an image that always fails on a specific device, attach it to this Thread and let us know what device and what OS you were testing.  We will have a look and see if we can determine what the problem is.