Mr. Sajid Shaikh
Mr. Sajid Shaikh

Reputation: 7251

How to download images in background in j2me?

I have one canvas where I am showing one image on center. This image is downloaded by url. Actually there are more images to be downloaded, that means if user clicks RIGHT I have to show next image if LEFT, I have to show another one image from backside. I have array of strings which storing images of url. I want to download previous and next image in background. How to do that?

Upvotes: 1

Views: 568

Answers (2)

Akshay Vats
Akshay Vats

Reputation: 170

Do something like..

Thread t = new Thread(new Runnable(){
  public void run() {
    // Your code to download image here as you were doing earlier,
    // for previous and next image
  }
});

t.start();

Upvotes: 0

Vimal
Vimal

Reputation: 1266

Following are some of the issues that you need to consider for this requirement to work across a range of devices

  1. What if the image is big compared to the device canvas size ? That is image dimension w * h is very high compared to canvas w * h ?
  2. Can low memory devices load such big images ?
  3. Inconsistent multi-thread model across devices. That is some devices it will allow you create 10 threads while in other it will stop at 2.
  4. Can my application support the fetch image type ? That is I am downloading .JPG / .GIF but my device does not support it.
  5. How to cache the images ? Do you want to load the same image again and again when you navigate to that image ?
  6. Do I use TCP / HTTP / RMI for downloading image content ?
  7. Will this code be portable across different flavors JVM for JavaME ?

and the list continues.....

Keeping in mind the above set of issues that you will land-up with a constructive answer to your problem is to create a network IO manager and a caching manager.

interface NetworkIoItem {
     Object sourceComponent;

     public void onImageDownload(Image image) {
           //Trigger the source 
     }
}

.

class NetworkIoManager extends Threads {
     Vector pendingRequestQueue;

     :
     :

     public void run() {
           //Wait on the pending request queue

           //Process all the elements in pending request queue
           //and again wait on this queue
     }
}

.

class CacheManager {
     Vector cachedContent;

     public boolean checkIfCached() {
          //Check in the cachedContent whether
          //this image exists if so return true;
     }

     public Image fetchImage() {
          //Check if the image is cached
          //if so return this cached image

          //else push a NetworkIoItem object to 
          //NetworkIOManager pending queue
     }
}

Now for each image (current, left or right) call the CacheManager.fetchImage(), this method will take care of providing you image either cached or downloaded from server. In the same method if the image is not cached it will add NetworkIoItem objbect to the NetworkIoManager pendingQueue and download it. On completion of download it will trigger NetworkIoItem.onImageDownload(...) method.

You can use J2ME Polish's Touch feature to download a image in the NetworkIoManager

By using this approach you will do asynchronous image fetches to the request URL.

Upvotes: 1

Related Questions