03-03-2011 01:15 PM
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.
03-03-2011 01:33 PM
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:
03-04-2011 11:00 AM
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
03-04-2011 11:23 AM
"Still got nullpointerexception"
And what line of code throws that exception?
03-04-2011 12:55 PM
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");
}});
03-04-2011 01:06 PM
Couldn't find it in the code so I'm guessing it's somewhere else, but did you remember to initialize your img Vector?
03-04-2011 02:18 PM
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.
03-04-2011 03:18 PM
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.
03-05-2011 02:34 AM
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);
}
}
03-07-2011 07:57 AM
Still not get solution for running Thread in Thread hmmm.
Do I miss something or use wrong invoke ??