Universal image downloader - use the same image in multiple views
I am using Universal Image Loader to load images from the backend to display custom images in a list; however, if the icon is displayed multiple times, the Universal Image Loader does not fill all views.
- [User Image 1] - No image
- [User Image 1] - No image
- [User Image 2] - Fine
- [User Image 2] - No image
- [Custom Image 3] - Fine
- [User Image 1] - Fine
And then on another screen:
- [User Image 1] - Fine
- [User Image 1] - No image
I am using cacheInMemory and cacheOnDisk which seemed to improve it. As before, it only showed up in one of the views, not most, but I need them all to work.
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.cacheInMemory( true )
.cacheOnDisk( true )
.build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder( this )
.threadPoolSize( 3 )
.defaultDisplayImageOptions( defaultOptions )
.build();
ImageLoader.getInstance().init( config );
I am not using a ListView for this task, I am using a ScrollView and inflating it with a custom layout.
private View createSmallActivity( LayoutInflater inflater, final Event activity ) {
final View view;
view = inflater.inflate( R.layout.activity_posted_small, null );
...
// The owner image.
if( activity.ownerImageUrl != null ) {
Loader.loadImage( getActivity(),
activity.ownerImageUrl,
R.drawable.postedactivitysmall_imageprofileempty,
( ImageView ) view.findViewById( R.id.profileImage ) );
}
return view;
}
// Loader.loadImage
// Setting the targetSize, and masking the image with a resource.
public static void loadImage( Context context, String url, int resource, ImageView view ) {
Drawable d = context.getResources().getDrawable( resource );
int h = d.getIntrinsicHeight();
int w = d.getIntrinsicWidth();
ImageSize targetSize = new ImageSize( w, h );
ImageLoader.getInstance().loadImage( url, targetSize, new MaskImageLoader( context, view, resource ) );
}
Any idea on how I can improve Universal Image Loader to ensure that all views are filled in correctly?
Thank you for your help!
This is because the Universal Image Loader cancels previous requests with the same url (used as id). To prevent this, replace
ImageLoader.getInstance().loadImage( url, targetSize, new MaskImageLoader( context, view, resource ) );
by
ImageLoader.getInstance().displayImage(url,
new NonViewAware(new ImageSize(w, h), ViewScaleType.CROP),
new MaskImageLoader(context, view, resource));
I recommend using ListView. Perhaps you should declare ImageLoader globally. then use it.
ImageLoader imageLoader = ImageLoader.getInstance();
...
imageLoader.displayImage(imageUri, imageView);