Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Regular Contributor
Posts: 97
Registered: ‎07-12-2011
My Device: curve 8900
My Carrier: Airtel

How to load images quickly like android

hi. im loading and display image from server. to loads the images its takes some time.. how to load images fastly..

Developer
Posts: 208
Registered: ‎08-04-2009
My Device: Not Specified

Re: How to load images quickly like android

YOu can use threads to load images when download completes.

Regular Contributor
Posts: 97
Registered: ‎07-12-2011
My Device: curve 8900
My Carrier: Airtel

Re: How to load images quickly like android

i have mlutiple images.. what im doing now i created method.. its every time give diff URL for load or read image.. suppose 5 image is there means i read one by one.. but i want read multiple images at same time from server
Developer
Posts: 208
Registered: ‎08-04-2009
My Device: Not Specified

Re: How to load images quickly like android

I am writing my code here, this code contains a thread which is responsible to download a bitmap

package mypackage;

import net.rim.device.api.system.Bitmap;

public class BitmapLazyLoader implements Runnable{
	String url=null;
	BitmapDowloadListener listener=null;

	public BitmapLazyLoader(String url, BitmapDowloadListener listener) {
		// TODO Auto-generated constructor stub
		this.url=url;
		this.listener=listener;
	}
	public void run() {
		// TODO Auto-generated method stub
		Bitmap bmpImage=getImageFromWeb(url);
		listener.ImageDownloadCompleted(bmpImage);
	}
	
	
	private Bitmap getImageFromWeb(String url2) {
		// TODO Auto-generated method stub
		//Code to Fetch Image from url
		return null;
	}

}

 

Listener Code:

 

import net.rim.device.api.system.Bitmap;

public interface BitmapDowloadListener {
public void ImageDownloadCompleted(Bitmap bmp);
}

 Copy above two classes and use following code in your screen, where you want to display these images.

 

for(int i=0;i<arrPictures.length;i++)
        {
        	String url=arrPictures[i];
        	BitmapLazyLoader loader=new BitmapLazyLoader(url, new BitmapDowloadListener()
        	{

				public void ImageDownloadCompleted(Bitmap bmp) {
					// TODO Auto-generated method stub
					bmpField.setBitmap(bmp);
				}
        		
        	});
        }

 

Regular Contributor
Posts: 97
Registered: ‎07-12-2011
My Device: curve 8900
My Carrier: Airtel

Re: How to load images quickly like android

Thanks for reply and ur code.. its really cool.. what is that arrPictures..
Developer
Posts: 208
Registered: ‎08-04-2009
My Device: Not Specified

Re: How to load images quickly like android

Just An array of Strings containing URLs of images you want to download.

Regular Contributor
Posts: 97
Registered: ‎07-12-2011
My Device: curve 8900
My Carrier: Airtel

Re: How to load images quickly like android

Thank u so much jitendrasharma.. its working pretty good.. i have last one doubt if use the thread.. Application gets any problem.. like memory management, etc.. In that for loop i started the thread.. after complete the loop.. how to stop the thread
Developer
Posts: 208
Registered: ‎08-04-2009
My Device: Not Specified

Re: How to load images quickly like android

Thread which completes its body in run method are stopped automatically.

But you may need to stop thread when you are poping your screen on stack, or push other screens on the stack.

 

Yes, there is an issue may arise due to threads, i.e. Too many threads, to avoid this issue I normally Use a single thread to download all the image.

Developer
Posts: 208
Registered: ‎08-04-2009
My Device: Not Specified

Re: How to load images quickly like android

To stop a thread, you just need to invoke interrupt() method of thread.

Regular Contributor
Posts: 97
Registered: ‎07-12-2011
My Device: curve 8900
My Carrier: Airtel

Re: How to load images quickly like android

Thanks dude