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 Visitor
Posts: 1
Registered: ‎07-27-2012
My Device: 9550
My Carrier: mobilink
Accepted Solution

EncodedImage is not working

i am trying to loading the image from the server in multithreading envoirnment:. i receive responsecode 200.and also receive the byte array.No:

but my i am unable to create EncodedImage

HttpConnection httpConnection = null;

                InputStream httpInput = null;
                int responseCode;


                try {

                    httpConnection = (HttpConnection)new ConnectionFactory().getConnection(url).getConnection();
                    httpConnection.setRequestProperty("User-Agent","Profile/MIDP-1.0 Confirguration/CLDC-1.0");
                    httpConnection.setRequestMethod(HttpConnection.GET);
                    responseCode=httpConnection.getResponseCode();
                                        if (responseCode != HttpConnection.HTTP_OK) {
                        throw new IOException("HTTP response code: " + responseCode);
                    }
                    httpInput = httpConnection.openInputStream();
                    

                    byte[] b = IOUtilities.streamToBytes(httpInput);

                 //problem is here this line is not execute
                    EncodedImage bitmap=EncodedImage.createEncodedImage(b, 0, b.length, "image/png");
                    if(bitmap!=null){
                        int dw = Fixed32.toFP(Display.getWidth());
                        int iw = Fixed32.toFP(bitmap.getWidth());
                        int sf = Fixed32.div(iw, dw);
                        final EncodedImage bitmap2 = bitmap.scaleImage32(sf * 4, sf * 4);

                        storeCacheData(url, bitmap2);
                        UiApplication.getUiApplication().invokeLater(new Runnable(){  

                            public void run(){  
                                if(bitmap2!=null)
                                    load.download(bitmap2);
                            }  

                        });
                    }


                } catch (Exception e) {
                    //allow thread to exit
                    e.printStackTrace();
                }


            }catch(Exception e){
                e.printStackTrace();
            }//end catch
        }

    }

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

Re: EncodedImage is not working

When you debug this, I presume that this line is executed

 

EncodedImage bitmap=

 

but following lines are not.  That is almost certainly because this has thrown an exception.  Unfortunately your catches, which do this:

 

                } catch (Exception e) {
                    //allow thread to exit
                    e.printStackTrace();
                }

will not display anything because the only way to output a printStackTrace in BlackBerry is to catch a Throwable, for example:

 

                } catch (Throwable t) {
                    //allow thread to exit
                    t.printStackTrace();
                }

I would put better logging in your exception catching (everywhere, not just here) and make sure you see something.

 

So then the obvious question is why is this conversion throwing an exception.  If you print the exception detail message, then this will probably help you figure this out.

 

Most likely it is because the data you receive is not what you expect.  The best thing to do is to compare, at a byte level, the contents of the bytes you have received and what you expect to get.  Make sure that it is in fact a png file.