Android - Delay While Loading Bitmaps Into Arraylist Using Universalimageloader
Solution 1:
You can't add the elements in the way you're trying to. If for example the 2nd image finishes loading first, your code will correctly throw an IndexOutOfBoundsException
as the location you're trying to add at is beyond the current size - see the documentation
You might be better off using an array initialised to the number of elements seeing as you know it's 4 elements - e.g.
final Bitmap[] imgArray = new Bitmap[4];
Then add the elements in your onLoadingComplete()
using
imgArray[constNum] = loadedImage;
Solution 2:
You can use a SparseArray
instead of an ArrayList
to avoid the IndexOutOfBoundsException
.
I assume this is what you are after:
final SparseArray<Bitmap> imgArray = newSparseArray<>(); //before the method scope, as a class fieldint numberOfImages;
int numberOfLoadedImages;
//...some code...FilecacheDir= StorageUtils.getOwnCacheDirectory(
getApplicationContext(),
"/sdcard/Android/data/random_folder_for_cache");
DisplayImageOptionsoptions=newDisplayImageOptions.Builder()
.cacheInMemory(true).cacheOnDisc(true).build();
ImageLoaderConfigurationconfig=newImageLoaderConfiguration.Builder(
getApplicationContext()).defaultDisplayImageOptions(options)
//.discCache(new FileCounterLimitedCache(cacheDir, 100)) - I commented it 'cause FileCounterLimitedCache isn't recognized for some reason
.build();
ImageLoader.getInstance().init(config);
numberOfImages = 4;
numberOfLoadedImages = 0;
for (int num=0;num<4;num++) {
ImageLoaderimageLoader= ImageLoader.getInstance();
finalintconstNum= num;
imageLoader.loadImage("http://example.com/sample.jpg", newSimpleImageLoadingListener()
{
@OverridepublicvoidonLoadingComplete(String imageUri, View view, Bitmap loadedImage)
{
imgArray.put(constNum, loadedImage);
numberOfLoadedImages++;
if(numberOfImages == numberOfLoadedImages)
{
//Do all the other actions
}
}
});
}
Solution 3:
Universal Image Loader library contains the following method:
publicvoiddisplayImage(java.lang.String uri, android.widget.ImageView imageView)
Which can be passes the image url and the ImageView
to display the image on it.
plus to that, if later the same ImageView
passed to the method but with a different url two thing can happen:
if old image already downloaded, normally the new image will be set to the ImageView.
if old image still downloading the library will cancel the http connection that is downloading the old image. and start downloading the new image. (can be observed in the LogCat). this behaviour happens when using an adapter.
Method call:
ImageLoader.getInstance().displayImage("http://hydra-media.cursecdn.com/dota2.gamepedia.com/b/bd/Earthshaker.png", imageView1);
Configuration:
Caching will not work if the defaultDisplayImageOptions not specified. which tells the library where to save those image. because this library has options to load images from Assets, Drawables or Internet:
DisplayImageOptionsopts=newDisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true).build();
With this option the images will be saved in app. internal memory. don't worry weather the device have an external memory or not.
ImageLoaderConfigurationconfig=newImageLoaderConfiguration.Builder(this).defaultDisplayImageOptions(opts).memoryCache(newLruMemoryCache(2*1024*1024)).memoryCacheSize(2*1024*1024).diskCacheSize(50*1024*1024).diskCacheFileCount(100).writeDebugLogs().build();ImageLoader.getInstance().init(config);
Post a Comment for "Android - Delay While Loading Bitmaps Into Arraylist Using Universalimageloader"