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

  • Home
  • SEARCH
  • 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 8363117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:06:11+00:00 2026-06-09T12:06:11+00:00

Every time when my apps went to the layout that download image online, the

  • 0

Every time when my apps went to the layout that download image online, the device will hang and need to wait the download finish only can movable.

I did some researched. They recommend download it in another Thread. However, I not understand how to implement the download function in another Thread.

Here is my code to call the download image function.

Main.getUiApplication().invokeLater(new Runnable() {
            public void run() {
                for (j = 0; j < imagepath.length; j++) {
                    if (!imagepath[j].toString().equals("no picture")
                            && Config_GlobalFunction.isConnected()) {
                        loader = new Util_LazyLoader(imagepath[j],
                                new Util_BitmapDowloadListener() {
                                    public void ImageDownloadCompleted(
                                            Bitmap bmp) {
                                        imagebitmap[j] = bmp;
                                        invalidate();
                                    }
                                });
                        loader.run();
                    }
                }
            }
        }, 500, false);

And the lazyloader

public class Util_LazyLoader implements Runnable {
String url = null;
Util_BitmapDowloadListener listener = null;

public Util_LazyLoader(String url, Util_BitmapDowloadListener listener) {
    this.url = url;
    this.listener = listener;
}

public void run() {
    Bitmap bmpImage = getImageFromWeb(url);
    listener.ImageDownloadCompleted(bmpImage);
}

private Bitmap getImageFromWeb(String url) {
    HttpConnection connection = null;
    InputStream inputStream = null;
    EncodedImage bitmap;
    byte[] dataArray = null;

    try {
        connection = (HttpConnection) (new ConnectionFactory())
                .getConnection(url + Database_Webservice.ht_params)
                .getConnection();

        int responseCode = connection.getResponseCode();
        if (responseCode == HttpConnection.HTTP_OK) {
            inputStream = connection.openDataInputStream();
            dataArray = IOUtilities.streamToBytes(inputStream);
        }
    } catch (Exception ex) {
    } finally {
        try {
            inputStream.close();
            connection.close();
        } catch (Exception e) {
        }
    }

    if (dataArray != null) {
        bitmap = EncodedImage.createEncodedImage(dataArray, 0,
                dataArray.length);
        return bitmap.getBitmap();
    } else {
        return null;
    }
}
}

I need help on it as I not familiar in networking.

  • 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-06-09T12:06:13+00:00Added an answer on June 9, 2026 at 12:06 pm

    So, the Util_LazyLoader is already well written to support background image downloads, because it implements the Runnable interface. You can start the download like this:

    Util_LazyLoader loader = 
        new Util_LazyLoader(imagepath[j],
                            new Util_BitmapDowloadListener() {
                                public void ImageDownloadCompleted(final Bitmap bmp) {
                                    UiApplication.getUiApplication().invokeLater(new Runnable() {
                                        public void run() {
                                            imagebitmap[j] = bmp;
                                            invalidate();
                                        }
                                    });
                                }
                            });
    
    Thread backgroundWorker = new Thread(loader);
    backgroundWorker.start();
    

    instead of directly calling the loader.run() method yourself.

    A Runnable class is just one that has a run() method. You give your Runnable loader object to a new Thread and tell it to start(). This will cause that Thread to execute the run() method in another thread, instead of the UI thread. As long as you don’t run network operations on the UI thread, your app should not appear to the user to be frozen.

    Note: in your original code, you have this:

    Main.getUiApplication().invokeLater(new Runnable() {
            public void run() {
    

    You probably don’t need that at all. If that code is being run from the main (UI) thread, then all that’s doing is telling the app to invoke that locally-defined run() method, also on the UI thread. You do pass a 500 millisecond delay as well. Maybe you need that (?). If you just want it to run right away, though, get rid of the code above (invokeLater(new Runnable() { public void run() { ...). Just use the code I posted (at the top of this answer) to create the backgroundWorker and then call its start() method.

    Also, take note of two things in my implementation:

    1. I used the UiApplication.invokeLater() method once the bitmap has been received. After the network operation completes, the UI must be updated. But that should not be done on the background thread. So, you create a Runnable to run on the background thread, and then once the download is complete, you create another Runnable to update the UI:

    public void run() {
       imagebitmap[j] = bmp;
       invalidate();
    }
    

    2. Because I create another Runnable, and use the bmp variable inside that Runnable, I must declare it as a final parameter. The compiler requires you to do that. Another option would be to use the event lock directly, instead of invokeLater():

    public void ImageDownloadCompleted(Bitmap bmp) {
        synchronized(UiApplication.getEventLock()) {
             imagebitmap[j] = bmp;
             invalidate();
        }
    }
    

    Either should work for you.

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

Sidebar

Related Questions

The stop executable dialog that comes up every time I have an app already
Is it possible to register a global broadcast receiver that gets notified every time
I just started to play with MemCache in Google Apps Engine and every time
While iOS developers submit their new apps in itunes connect, Apple every time asks
I have an app that downloads several resources from the Internet every time it
Every time I log on to my server through SSH I need to type
Using Lookout app (https://play.google.com/store/apps/details?id=com.lookout), I see every time I install or upgrade app, it'll
Every time i reset & seed my database it wipes out the standard admin@example.com
Every time when I'm trying to load the .dll in my application, I'm getting
every time I open my Solution in Visual Studio it tries to communicate and

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.