Welcome to the Official BlackBerry® Support Community Forums. This is your resource to discuss support topics with your peers, and learn from each other. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Java Development

Reply
Regular Contributor
gillaraz
Posts: 69
Registered: 10-19-2010

Program not response when try to get image from URL

I am using listfield and implementing ListFieldCallback for listfield.

 

I sent img url to listCallback

 

class listCallback implements ListFieldCallback{ 

 

public void drawListRow(ListField list, Graphics g, int index, int y, int w)

 

public void insert(String title,String teaser,String thumb, int index, String type)

 

public Bitmap getBitmap(String url)

}

 

and take it to

 

 

public Bitmap getBitmap(String url) throws IOException {
	
    InputStream is = (InputStream) Connector.openInputStream(url);
    
    	Bitmap pic;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int ch;
        while ((ch = is.read()) != -1) 
            baos.write(ch);
        
        byte imageData[] = baos.toByteArray();
        pic = Bitmap.createBitmapFromBytes(baos.toByteArray(), 0, baos.toByteArray().length, 1); 
        	

		return pic;

}

 

And I have used

 

 

Bitmap x = getBitmap(thumbURL);

 

to get image and then go to drawListRow(); in ListCallBack

 

When I open the application, it is not response at all.

 

I need some help plz.

 

Please use plain text.
Developer
arkadyz
Posts: 2,146
Registered: 07-08-2009
My Carrier: various

Re: Program not response when try to get image from URL

It is not exactly clear from your description, but I suspect you are downloading that image right there in the Event Thread which is a very bad thing.

 

Delegate the download to a separate Thread, and, when the download is complete, tell your ListField to invalidate() itself. Meanwhile, have your drawListRow check whether the Bitmap reference has been populated - and, if yes, drawBitmap in the correct place. If it is still null, though, just skip that part.

 

To learn about Event Thread, take a look here:

What is the Event Thread?

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Please use plain text.
Regular Contributor
gillaraz
Posts: 69
Registered: 10-19-2010

Re: Program not response when try to get image from URL

 



public void getBitmap(final String url) throws IOException { Thread t = new Thread(new Runnable(){ public void run() { // TODO Auto-generated method stub try { InputStream is; is = (InputStream) Connector.openInputStream(url); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); int ch; while ((ch = is.read()) != -1) baos.write(ch); byte imageData[] = baos.toByteArray(); UiApplication.getUiApplication().invokeLater(new Runnable() { public void run() { Bitmap pic; pic = Bitmap.createBitmapFromBytes(baos.toByteArray(), 0, baos.toByteArray().length, 1); img.addElement(pic); } } ); } catch (IOException e) { e.printStackTrace(); } }}); t.start(); } }

 

Ok then I tried this one add bitmap to vector img and paint in DrawListRow.

 

Still got nullpointerexception

 

Please use plain text.
Developer
arkadyz
Posts: 2,146
Registered: 07-08-2009
My Carrier: various

Re: Program not response when try to get image from URL

"Still got nullpointerexception"

 

And what line of code throws that exception?

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Please use plain text.
Regular Contributor
gillaraz
Posts: 69
Registered: 10-19-2010

Re: Program not response when try to get image from URL

I throw any exception IOException.

 

This is when I call the Thread to fetch image.

 

 

 

UiApplication.getUiApplication().invokeAndWait(new Runnable() {

	    	public void run() {

	    		getBitmap("http://www.eng.chula.ac.th/files/building.jpg");
      
	    	}});

 

 

 

 

 

 

 

 

 

 

 

Please use plain text.
Developer
jprofitt
Posts: 604
Registered: 12-27-2010

Re: Program not response when try to get image from URL

Couldn't find it in the code so I'm guessing it's somewhere else, but did you remember to initialize your img Vector?

Please use plain text.
Regular Contributor
gillaraz
Posts: 69
Registered: 10-19-2010

Re: Program not response when try to get image from URL

This is what I have initialize

 

private Vector img = new Vector();

 

Now the NullPointerexception is solve but I found that there is no Bitmap in vector img at all when I use this.

 

if(img.size()!=0)
                    g.drawBitmap(0, y, 75, 75, (Bitmap)img.elementAt(0), 0, 0);

 

 

 

Seems this thread doesn't success to get image from url.

Please use plain text.
Developer
arkadyz
Posts: 2,146
Registered: 07-08-2009
My Carrier: various

Re: Program not response when try to get image from URL

No, the Thread might still be downloading the image. Your paint is first called when the field needs to be displayed. Since you  have your download in a separate Thread, you might well need to paint before the image arrives. If that's the case, your paint() will simply skip the image drawing part.

 

In order to redraw the screen when the image arrives, invalidate() the field after getting that Bitmap.

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Please use plain text.
Regular Contributor
gillaraz
Posts: 69
Registered: 10-19-2010

Re: Program not response when try to get image from URL

Ahh, I understand your point but invalidate() is not working for me.

 

this is my constructor of listcallback class

 

 

 public listCallback(ListField list) {
	    // save a ref to the list view
	    _view = list;

	    // bind this model to the given view
	    list.setCallback(this);

	     //set the default row height
	    _view.setRowHeight(_defaultRowHeight);

	    
	  }

 

And Itried with this method, Still got nothing

 

 

 

public void getBitmap(final String url,final String title, String teaser, final int index)   {

_view.insert(index);
final Thread t = new Thread(new Runnable(){

public void run() {
// TODO Auto-generated method stub
try {
InputStream is;
is = (InputStream) Connector.openInputStream(url);

final ByteArrayOutputStream baos = new ByteArrayOutputStream();
int ch;

while ((ch = is.read()) != -1)
baos.write(ch);

final byte imageData[] = baos.toByteArray();

UiApplication.getUiApplication().invokeAndWait(new Runnable() {

public void run() {

Bitmap pic;
pic = Bitmap.createBitmapFromBytes(imageData, 0, imageData.length, 1);

img.insertElementAt(pic, 0);


}
}
);


} catch (IOException e) {

e.printStackTrace();
}
}});

UiApplication.getUiApplication().invokeAndWait(new Runnable() {

public void run() {

t.start();

}});

data.insertElementAt(title, index);
data2.insertElementAt(teaser, index);
_view.invalidate(index);



}
}

 

 

Please use plain text.
Regular Contributor
gillaraz
Posts: 69
Registered: 10-19-2010

Re: Program not response when try to get image from URL

Still not get solution for running Thread in Thread hmmm.

 

Do I miss something or use wrong invoke ??

Please use plain text.