问题
I need to load Bitmaps into ArrayList, then convert it to Bitmap[] and pass into ArrayAdapter to inflate ListView. I use UniversalImageLoader library and here is my code:
final ArrayList<Bitmap> imgArray = new ArrayList<>(); //before the method scope, as a class field
//...some code...
File cacheDir = StorageUtils.getOwnCacheDirectory(
getApplicationContext(),
"/sdcard/Android/data/random_folder_for_cache");
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheInMemory(true).cacheOnDisc(true).build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.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);
for (int num=0;num<4;num++) {
ImageLoader imageLoader = ImageLoader.getInstance();
final int constNum = num;
imageLoader.loadImage("http://example.com/sample.jpg", new SimpleImageLoadingListener()
{
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage)
{
imgArray.add(constNum, loadedImage);
}
});
}
But I have some issues to struggle. Firstly, it often returns error (IndexOutOfBoundsException - current index exceeds size of ArrsyList) when first run. Then after some time (about a minute) the size of ArrayList is 1 (I check it with Toast), and then when run again right at once, it's already 4 (as it needs to be). Strange. But the main thing I need that the ArrayList is first filled and then all the other actions are done (that they be delayed and I don't have errors on the first run). How to do it?
And what to do if somebody doesn't have SD card? Btw, I couldn't find the created cache folder on my SD...
回答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;
回答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 = new SparseArray<>(); //before the method scope, as a class field
int numberOfImages;
int numberOfLoadedImages;
//...some code...
File cacheDir = StorageUtils.getOwnCacheDirectory(
getApplicationContext(),
"/sdcard/Android/data/random_folder_for_cache");
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheInMemory(true).cacheOnDisc(true).build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.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++) {
ImageLoader imageLoader = ImageLoader.getInstance();
final int constNum = num;
imageLoader.loadImage("http://example.com/sample.jpg", new SimpleImageLoadingListener()
{
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage)
{
imgArray.put(constNum, loadedImage);
numberOfLoadedImages++;
if(numberOfImages == numberOfLoadedImages)
{
//Do all the other actions
}
}
});
}
回答3:
Universal Image Loader library contains the following method:
public void displayImage(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:
DisplayImageOptions opts = new DisplayImageOptions.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.
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
.defaultDisplayImageOptions(opts)
.memoryCache(new LruMemoryCache(2 * 1024 * 1024))
.memoryCacheSize(2 * 1024 * 1024)
.diskCacheSize(50 * 1024 * 1024)
.diskCacheFileCount(100)
.writeDebugLogs()
.build();
ImageLoader.getInstance().init(config);
I created a working github repository for it. check it out.
来源:https://stackoverflow.com/questions/31777399/android-delay-while-loading-bitmaps-into-arraylist-using-universalimageloader