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
New Developer
yuzman
Posts: 8
Registered: ‎09-18-2009
My Device: Not Specified
Accepted Solution

Popup Screen cannot display a picture

Hello all, for the possible questions : 

- I'm using eclipse with blackberry plugin

- The simulator is 8320

- OS 4.5

 

So here's the question:

I want to develop an application which provides the user to add several pictures to a list and then send them over a connection. The connection part seems complete but I want to add a feature.

 

- You took a picture (with camera no additional application)

- By using my application you go into the picture's directory.

- You select the picture.

- After the selection I want to display a popup screen showing the picture. Unfortunately I was unable to do that. For further questions I'm posting the code here. Note that I'm constantly getting a Nullpointer exception from the fileconnection but I use the same class for directory listing and so, and no exceptions thrown. Thanks already for the fast replies. 

 

pushScreen(new PopupScreen(null)

                 {

                 {

                 try {

                            FileConnection fconn = (FileConnection)Connector.open("file:///SDCard/BlackBerry/pictures/test.jpg");

                            // If no exception is thrown, then the URI is valid, but the file may or may not exist.

                            if (fconn.exists()) {

                                InputStream input = fconn.openInputStream();

                                int available = input.available();

                                byte[] data = new byte[available];

                                input.read(data, 0, available);

                                EncodedImage image = EncodedImage.createEncodedImage(data,0,data.length);

                                Bitmap b = image.getBitmap();

                                BitmapField picture = new BitmapField(b);

                                this.add(picture);

                                this.add(new LabelField("Data Length:" + data.length));

                            }

                            else {

                                this.add(new LabelField("Picture does not exist"));

                            }

                            fconn.close();

                        }

                        catch (Exception ioe) {

                            this.add(new LabelField("Error"));

                        }

                 };

                 }

                 ); 

Developer
bikas
Posts: 984
Registered: ‎02-10-2009
My Device: Not Specified

Re: Popup Screen cannot display a picture

Have a look at this article. You will get some idea how to use Popupscreen appropriately.

 

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800505/800256/...

 

Regards

Bikas

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

Re: Popup Screen cannot display a picture

"constantly getting a Nullpointer exception from the fileconnection" - at first glance, your code looks fine.  Which line actually see the exception.  And what is null?

 

Couple of things:

 

a) for the length, I would actually use

int available = (int) fconn.fileSize();

 

b) I would scale the image before displaying it, otherwise you will only see a small portion of it.

See

Bitmap.createBitmapFromBytes(...);

 

 

New Developer
yuzman
Posts: 8
Registered: ‎09-18-2009
My Device: Not Specified

Re: Popup Screen cannot display a picture

thanks for the fast replies. I've debugged the program and found where the null pointer is coming from. 

EncodedImage image = EncodedImage.createEncodedImage(data,0,data.length);

 

this line causes the null pointer exception, but because of the  'avaliable' returns 0. I didn't understand a thing, how come the explorer can find the file and this program cannot read the file ? I've considered the permissions maybe, but it seems like all are set to allow. any other ideas ?

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

Re: Popup Screen cannot display a picture

As I said, use fileSize rather than

int available = input.available();

 

available() is the number of bytes available without blocking.  0 is correct, there are no bytes available without blocking your processing to read the file.

New Developer
yuzman
Posts: 8
Registered: ‎09-18-2009
My Device: Not Specified

Re: Popup Screen cannot display a picture

Try that, didn't work. So here's the new code, and the new exception: got an OutOfMemoryException. Any ideas ? 

 

FileConnection fconn = (FileConnection)Connector.open("file:///SDCard/BlackBerry/pictures/test.jpg/");

 

            // If no exception is thrown, then the URI is valid, but the file may or may not exist.

            if (fconn.exists()) {

 

             InputStream input = fconn.openInputStream();                

             int j = 0 ;

                ByteArrayOutputStream baos = new ByteArrayOutputStream();

                byte[] data = null;

                while((j=input.read())!=-1)

                {

                 baos.write(j);

                }

 

                data = baos.toByteArray();

                input.close();

 

                EncodedImage image = EncodedImage.createEncodedImage(data,0,data.length);

                Bitmap b = image.getBitmap();                   <---- I was able to debug until this line of code. 

                currentpicture = new BitmapField(b);    

                this.add(currentpicture);

 

            } 

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

Re: Popup Screen cannot display a picture

Read pont (b) from my original post.