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
Posts: 4
Registered: ‎01-02-2010
My Device: Not Specified
Accepted Solution

Unable to display an online image

Hi all,

I am a newbie in BB app design and this is my first post in this forums. Please correct me if I got anything wrong.

 

I am using JDE 5.0 and BB 9550 simulator. I want to design a simple app to display an image online. I went through the forum and get the basic idea how to do it. The following is my code for this simple app.

 

 

public class HttpImageDemo extends UiApplication {

public HttpImageDemo() {
HttpImageScreen screen = new HttpImageScreen();
pushScreen(screen);
}

public static void main(String[] args) {
HttpImageDemo app = new HttpImageDemo();
app.enterEventDispatcher();
}
}

class HttpImageScreen extends MainScreen {


private HorizontalFieldManager headerManager;
private static EncodedImage _eImage;


public HttpImageScreen() {

super(NO_VERTICAL_SCROLL | DEFAULT_MENU);

LabelField demoText = new LabelField("Image Screen",
LabelField.HCENTER | LabelField.USE_ALL_WIDTH);

headerManager = new HorizontalFieldManager();
headerManager.add(demoText);

InputStream iStream;
try {
HttpConnection httpConnection = (HttpConnection) Connector
.open("http://upload.wikimedia.org/wikipedia/commons/1/1e/Stonehenge.jpg",
Connector.READ);

httpConnection.setRequestProperty("Accept","text/html, image/jpg, image/jpeg, image/");

httpConnection.setRequestMethod(HttpConnection.GET);

int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpConnection.HTTP_OK) {
iStream = httpConnection.openInputStream();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

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

while (-1 != (len = iStream.read(buffer))) {
byteArrayOutputStream.write(buffer);
imageSize += len;
}

byteArrayOutputStream.flush();
byte[] imageData = byteArrayOutputStream.toByteArray();
byteArrayOutputStream.close();

_eImage = EncodedImage.createEncodedImage(imageData, 0,
imageSize);
}

} catch (Exception e) {
add(new LabelField("Error"));
}

BitmapField bf = new BitmapField();
bf.setImage(_eImage);

add(headerManager);
add(new SeparatorField());
add(bf);
}

public boolean onClose() {
System.exit(0);
return true; }
}

 

When I click this application in the simulator, the system idles for about 1min and popup and message “Uncaught exception:java.langNullPointerException”. Thus I never get it work.

 

Can anyone give me any correction for my code and I am also glad to read any relative information. Your advice would be appreciated.


Thanks

 

 

Developer
Posts: 1,807
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.3.2.858, Z10 (STL100-3)-10.3.1.2576, Z30 (STA100-5)-10.3.1.2582, Passport (SQW100-1)-10.3.1.2576, PlayBook (16GB)-2.1.0.1917
My Carrier: Verizon

Re: Unable to display an online image

I'm guessing that the HttpConnection tries to access the image, can't, times out, returns a null HttpConnection, then when you try setRequestProperty it throws the exception. Are you running the MDS simulator? If so, where is the exception being thrown?

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: Unable to display an online image

[ Edited ]

You'll need to do a little bit better job of catching the potential exceptions. You might want to use System.out.println to put the exception message out in the output window.

 

One other observation: you are performing the HTTP IO in the event thread. This is not a good practice, since performing any type of blocking operation in the event thread can result in the event queue overflow, which will cause the OS to terminate your app as "non-responsive".

 

The likely scenario in your code is that the open request fails, probably with "not allowed in event thread", and your image variable is consequenly null, causing the NPE when you attempt to create the BitmapField.

 

 

There is a working (though not perfect) example here, which may be helpful:

 

http://www.coderholic.com/blackberry-webbitmapfield/

 

 

New Developer
Posts: 4
Registered: ‎01-02-2010
My Device: Not Specified

Re: Unable to display an online image

[ Edited ]

Thank both of you. I try to catch exception but I cannot get anything. I use both debug perspective and System.out.println but it always shows "Uncaught: RuntimeException" after about 1 min in the 9550 simulator. I am really sure that the image URL does exist.

 

I went through most of the posts in this forum but still have no idea about my problem. Can anyone give me any suggestion how to catch exception or problem of my code?

 

Thank you again.

Developer
Posts: 87
Registered: ‎07-23-2009
My Device: Z30
My Carrier: XL

Re: Unable to display an online image

[ Edited ]

hi,

I think you need to create connection in a separate thread (i.e create new thread for http connection), not in the event thread.

 

and don't forget to sart MDS simulator Smiley Wink

Developer
Posts: 222
Registered: ‎11-11-2009
My Device: Storm

Re: Unable to display an online image

[ Edited ]

Hi cwcheng83 

    Problem is at opening the HTTP connection. Since you are running the code in the simulator, have to make the connection to be made using direct TCP connection and which will not use Blackberry MDS connection service. so your code should be like to run in simulator

 

 

                InputStream iStream;
try {
HttpConnection httpConnection = (HttpConnection) Connector
.open("http://upload.wikimedia.org/wikipedia/commons/1/1e/Stonehenge.jpg;deviceside=true",
Connector.READ);

httpConnection.setRequestProperty("Accept","text/html, image/jpg, image/jpeg, image/");

this code to run in real device

		InputStream iStream;
try {
HttpConnection httpConnection = (HttpConnection) Connector
.open("http://upload.wikimedia.org/wikipedia/commons/1/1e/Stonehenge.jpg;deviceside=false",
Connector.READ);

httpConnection.setRequestProperty("Accept","text/html, image/jpg, image/jpeg, image/");

 

Edited: refer following link for more options

         http://www.blackberry.com/developers/docs/4.7.0api/javax/microedition/io/Connector.html

 

with regards,
Vignesh J

-------------------------------------------------------------------------------------------------------------------------------------------------------------------
Don't forget to mark your post as solved if you get the answer and dont forget to give kudos if the answer is useful for you.
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
New Developer
Posts: 4
Registered: ‎01-02-2010
My Device: Not Specified

Re: Unable to display an online image

Hi Zany,

 

Your way works for me. Besides, it also works when I fire on MDS. I was wondering which way is better? Thank you anyway.

Highlighted
Developer
Posts: 222
Registered: ‎11-11-2009
My Device: Storm

Re: Unable to display an online image

Hi cwcheng83,

        It is based on your requirement and application what you are developing. For me, i will prefer via MDS.

with regards,
Vignesh J

-------------------------------------------------------------------------------------------------------------------------------------------------------------------
Don't forget to mark your post as solved if you get the answer and dont forget to give kudos if the answer is useful for you.
-------------------------------------------------------------------------------------------------------------------------------------------------------------------