01-03-2012 02:33 AM
hi. im loading and display image from server. to loads the images its takes some time.. how to load images fastly..
01-03-2012 03:39 AM
YOu can use threads to load images when download completes.
01-03-2012 04:08 AM
01-03-2012 04:30 AM
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);
}
});
}
01-03-2012 05:40 AM
01-03-2012 05:43 AM
Just An array of Strings containing URLs of images you want to download.
01-03-2012 07:32 AM
01-03-2012 07:37 AM
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.
01-03-2012 07:38 AM
To stop a thread, you just need to invoke interrupt() method of thread.
01-03-2012 07:51 AM