Hi guys i am trying an sample where i have a button which downloads an image and then it need to render on screen. I am using the following code to get it done. Don’t know somewhere i am going real wrong. it throws me illegal Exception. can anyone please look at my code and provide me some help.
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import net.rim.device.api.system.Application;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.container.MainScreen;
public class GoogleChart extends MainScreen implements FieldChangeListener{
ButtonField btn = new ButtonField("Download");
GoogleChart activeScreen = null;
public GoogleChart() {
setTitle("Download image");
btn.setChangeListener(this);
add(btn);
this.activeScreen = (GoogleChart)UiApplication.getUiApplication().getActiveScreen();
}
public void fieldChanged(Field field, int context) {
if(field == btn){
updateUI();
}
}
private void updateUI(){
synchronized (Application.getEventLock()) {
activeScreen.add(new BitmapField(downloadImage()));
activeScreen.invalidate();
}
}
public Bitmap downloadImage() {
byte[] dataArray;
InputStream input;
StringBuffer url = new StringBuffer("IMAGE URL");
HttpConnection httpConn = null;
Bitmap googleImage = null;
try {
httpConn = (HttpConnection) Connector.open(url.toString());
input = httpConn.openInputStream();
dataArray = net.rim.device.api.io.IOUtilities.streamToBytes(input);
googleImage = Bitmap.createBitmapFromBytes(dataArray, 0, -1, 1);
} catch (IOException e) {
e.printStackTrace();
}
return googleImage;
}
}
Note: Image download works fine. even i have tested with other sample.
As per Dan suggestion i have changed the following code and separated the downloading logic into another thread.
class Download extends Thread{
Bitmap googleImage = null;
private void updateUI(){
synchronized (Application.getEventLock()) {
if(googleImage != null){
activeScreen.add(new BitmapField(googleImage));
activeScreen.invalidate();
}
}
}
public void run() {
googleImage = downloadImage();
updateUI();
}
public Bitmap downloadImage() {
byte[] dataArray;
InputStream input;
StringBuffer url = new StringBuffer("http://");
HttpConnection httpConn = null;
Bitmap googleImage = null;
try {
httpConn = (HttpConnection) Connector.open(url.toString());
input = httpConn.openInputStream();
dataArray = net.rim.device.api.io.IOUtilities.streamToBytes(input);
googleImage = Bitmap.createBitmapFromBytes(dataArray, 0, -1, 1);
} catch (IOException e) {
e.printStackTrace();
}
return googleImage;
}
}
and calling the thread like this
public void fieldChanged(Field field, int context) {
if(field == btn){
new Download().start();
}
}
What exception exactly do you get (does it have any message?)? At what step/line? As a possible case you may get an illegal exception if your
Bitmapis too big to create aBitmapFieldinstance. What dimensions (width x heigh) has yourBitmap? In my experience starting from ~1.5 Mpx it becomes impossible to createBitmapField(an exact limit depends on device model/OS level).UPDATE:
OK, then to figure out what is going on could you try the following approach?
What messages do you see? If you get “BitmapField has been added OK” then just remove all alerts (just leave the only in the
catchsection – just in case) and it should work Ok.