I'm developing quiz app. All questions are in server side. When user selects the category of quiz server returns the json file with questions, answer variants and image urls. For example in one category I have 10 questions and 4 questions of them have image to load. I check from json file which of them have image url and I need to download them to cache or to disk memory(don't know which solution is better)
The problem: How to load bunch of images from server and show progress bar? And how to access downloaded images when user wants to take a quiz?
I need to download images before starting answer to questions because there will be a time limit to answer the question.
I think you use https://github.com/nostra13/Android-Universal-Image-Loader. This library use disk cache to store images, so if the image is available on disk, you don't download the image.
To create a progress bar with update, you have to create a AsyncTask: http://developer.android.com/reference/android/os/AsyncTask.html.
The first example of the article is a download of a file. For your project, call publishProgress() at each image downloaded.
EDIT:
private class DownloadImageTask extends AsyncTask<List<URL>, Integer, List<Bitmap>> {
ImageLoader imageLoader = ImageLoader.getInstance(); // Instance android-universal-image-loader
List<Bitmap> downloadedImage = new LinkedList<>();
protected List<Bitmap> doInBackground(List<URL>... urls) {
List<URL> yoururls = urls[0];
for(URL url : yoururls){
imageLoader.loadImage(url.toString(), new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
downloadedImage.add(loadedImage);
publishProgress(1);
}
});
}
return downloadedImage;
}
protected void onProgressUpdate(Integer... progress) {
//Update here your progress bar
}
protected void onPostExecute(List<Bitmap> result) {
//Close your progress bar
//Put your bitmap in your imageview
}
protected void onPreExecute(){
//Create your progress bar
}
}
To execute:
List<Bitmap> yourimages = new DownloadImageTask().execute(YOURLISTURL).get();