List Of Images from Same Server
I am reading the List of Images from same Server, but Image Names are different.
for(int i=0;i<moviesL.size();i++)
{
try
{
lName.add(moviesL.get(i).getMovieName());
URL url = new URL(moviesL.get(i).getImageUrl());
URLConnection conn = url.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
images.add(BitmapFactory.decodeStream(bis));
}
catch (Exception e)
{
开发者_如何学编程 e.printStackTrace();
}
}
It is taking too much time to load.
is there any alternate this issue?
You can make the downloading multithreaded:
First you have to create a Thread class which downloads the image and stores it in the list of images.
class GetImageThread extends Thread {
URL url;
LinkedList<Bitmap> images;
public GetImageThread(URL url, LinkedList<Bitmap> images) {
this.url = url;
this.images = images;
}
public void run() {
try {
URLConnection conn = this.url.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
this.images.add(BitmapFactory.decodeStream(bis));
} catch (Exception e) {
e.printStackTrace();
}
Then you start a thread for each movie in the main program:
LinkedList<GetImageThread> threads = new LinkedList<GetImageThread>();
for(int i=0;i<moviesL.size();i++) {
GetImageThread thread = new GetImageThread(moviesL.getImageURL(), images);
threads.add(thread);
thread.start();
}
Then you have to wait for all threads to be finished.
for(GetImageThread thread : threads) {
thread.join();
}
And one last tip in the end: It is always better to use the iterator loop instead of the int loop.
for(Movie movie : movies)
instead of
for(int i=0; i<movies.size(); i++)
精彩评论