Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6611171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:56:09+00:00 2026-05-25T19:56:09+00:00

Hi guys i am trying an sample where i have a button which downloads

  • 0

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();
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:56:10+00:00Added an answer on May 25, 2026 at 7:56 pm

    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 Bitmap is too big to create a BitmapField instance. What dimensions (width x heigh) has your Bitmap? In my experience starting from ~1.5 Mpx it becomes impossible to create BitmapField (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?

    private void updateUI() {
        UiApplication.getUiApplication().invokeLater(new Runnable() {
            public void run() {
                if (googleImage == null) {
                    Dialog.alert("googleImage is null");
                } else {
                    Dialog.alert("googleImage is not null");
                    try {
                        BitmapField b = new BitmapField(googleImage);
                        Dialog.alert("BitmapField has been created OK");
                        activeScreen.add(b);
                        Dialog.alert("BitmapField has been added OK");
                        // no need to invalidate, since adding a new Field forces
                        // the screen to be repainted
                    } catch (Exception e) {
                        Dialog.alert("Got error: " + e);
                    }
                }
            }
        });
    }
    

    What messages do you see? If you get “BitmapField has been added OK” then just remove all alerts (just leave the only in the catch section – just in case) and it should work Ok.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello Guys! I have been trying to create a simple sample code for my
What I try to do Hello Guys, I'm trying to create a Login-Screen which
Hello guys! I have been trying to create a if/else statement code in jQuery
Hi guys I'm trying the code samples from zend frameworks site on how to
Hey guys I'm trying to make a GUI which can navigate through JTextAreas when
Hey guys I have been trying different Javascript/AJAX jsf frameworks. I find most of
Hey guys, this should be quite simple to answer.... Im using XSB Prolog...trying to
I'm trying to implement an MVP pattern using STL and I have used *shared_ptr*
I'm having a hard time trying to solve this. Here is my sample code:
Hi guys i am trying to create a simple chat server. I have installed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.