load image once with universal image loader in listview - android

I have a list view and I want to show thumbnail of video in it but everytime I scroll images download again. how can I download images once and show it.
this is my code:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null){
rootView = mInflater.inflate(R.layout.adapter_video, parent, false);
holder = new ViewHolder();
holder.ivThumbnail = (ImageView) rootView.findViewById(R.id.ivThumbnailVideoAdapter);
rootView.setTag(holder);
}
else{
rootView = convertView;
holder = (ViewHolder)rootView.getTag();
}
imageLoader.displayImage(update.get(position).getThumbnail(), holder.ivThumbnail, option);
return rootView;
}

with UIL the download of the images is configured with DisplayImageOptions
For example :
mOptionsSimple = new DisplayImageOptions.Builder().resetViewBeforeLoading(true)
.cacheOnDisc(true)
.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
Here, cacheOnDisc to true saves the image on the disk, not in the cache. When you scroll the image will reload, of course because in listview cells are recycled. But with this option it will reload from the disk if it was already downloaded
You can also/or enable memoryCache BUT be carefull with this one. It can be the cause of OutOfMemoryException if the images are not managed correclty
also ImageLoaderConfiguration here is an example :
config = new ImageLoaderConfiguration.Builder(getApplicationContext()).threadPoolSize(3)
.threadPriority(Thread.NORM_PRIORITY - 1)
.tasksProcessingOrder(QueueProcessingType.FIFO)
.imageDownloader(new MyImageDownloader(mContext))
.imageDecoder(new BaseImageDecoder(false))
.discCache(new UnlimitedDiscCache(cacheDir))
.discCacheFileNameGenerator(new HashCodeFileNameGenerator())
.defaultDisplayImageOptions(mOptionsSimple)
.writeDebugLogs()
.build();
In this last example the discCache is not limited which could be changed if you think it ll take too much space on the disk

Related

Android - GridView scroll is reloading images in universal image loader

I'm using universal image loader, but the images are reloaded every time I scroll up and down.
I'm using below code.
Configuration - >
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
.memoryCache(new WeakMemoryCache())
.denyCacheImageMultipleSizesInMemory()
.discCacheFileNameGenerator(new Md5FileNameGenerator())
.tasksProcessingOrder(QueueProcessingType.LIFO)
// .enableLogging() // Not necessary in common
.build();
// Initialize ImageLoader with configuration.
ImageLoader.getInstance().init(config);
Options->
options = new DisplayImageOptions.Builder()
.resetViewBeforeLoading()
.imageScaleType(ImageScaleType.EXACTLY)
.cacheInMemory(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
view ->
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View workingview = inflater.inflate(R.layout.gridview_item_photo_icon, null);
ImageView imageView = (ImageView) workingview.findViewById(R.id.picture);
ImageAware imageAware = new ImageViewAware(imageView, false);
imageLoader.displayImage("file://" + imageUrls.get(position), imageAware, options, new SimpleImageLoadingListener());
return workingview;
}
Please let me know where I'm going wrong.
There is a limit of chache in android around 1MB of image cashing it clears cache and download again that is why it reloads again images.
Its is proper way if you are not downloading images in your SD card of phone memory.
You can not hold much data in caching.

Cache downloaded image in ListView

I have a ListView with an adapter that loads various data from an API. One of the data is a URL to an image online. I download the image and set it in my ImageView in the adapter.
The problem is though, when I scroll up and down in the ListView, images are re-downloaded and there is a significant lag when doing that. Also, for a split second, it incorrectly displays the wrong image.
I need to somehow cache these images to only download once and properly set them in that ListView - How do I do that?
EDIT:
I use universal image loader to load my images like this:
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.displayImage(imageUrl, viewHolder.thumbnailImageView);
But it does not seem to cache them... Do I have to do something additional?
Use ImageLoaders
Glide
ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
Glide.with(this).load("http://goo.gl/gEgYUd").into(imageView);
Simple Image load. Check the link you can use them with listeners.
You can also try:
picasso
Android-Universal-Image-Loader
EDIT: As the OP said he uses UIL .
]You should use Image Aware.
You can have your own configuration with high cache memory something like this.
public void initializeImageLoader(final Context sContext) {
final ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(sContext)
.memoryCacheExtraOptions(480, 800) // default = device screen dimensions
.taskExecutor(AsyncTask.THREAD_POOL_EXECUTOR)
.taskExecutorForCachedImages(AsyncTask.THREAD_POOL_EXECUTOR)
.threadPoolSize(3) // default
.threadPriority(Thread.NORM_PRIORITY - 1) // default
.tasksProcessingOrder(QueueProcessingType.FIFO) // default
.denyCacheImageMultipleSizesInMemory()
.memoryCache(new UsingFreqLimitedMemoryCache(50 * 1024 * 1024)) // default
.memoryCacheSize(50 * 1024 * 1024)
.imageDownloader(new BaseImageDownloader(sContext)) // default
.defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
.build();
ImageLoader.getInstance().init(config);
}
Use this initialization before you get instance.
initializeImageLoader(context);
ImageLoader imageLoader = ImageLoader.getInstance();
Set Options for your loader.
final DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.loading)
.showImageForEmptyUri(R.drawable.noimage)
.showImageOnFail(R.drawable.noimage)
.resetViewBeforeLoading(false) //default
.bitmapConfig(Bitmap.Config.RGB_565)
.cacheInMemory(true)
.cacheOnDisk(false) //default
.build();
And set it to your loader:
final ImageAware imageAware = new ImageViewAware(viewHolder.thumbnailImageView, false);
imageLoader.displayImage(imageUrl, imageAware, options);

Bitmap Image not loading (Blank)

I have this issue with loading my Bitmap image. I created a custom ImageAdapter and ImageItem for my GridLayout view. The application is supposed to take a picture, save it to the external storage, and retrieve the image from the external storage, showing it as a small thumbnail in a grid. When that image is clicked from the grid, a new Activity is started, showing the full-sized image. Everything has worked fine so far (The images get saved, and I can actually view them on the file system, the supplied path is correct), but the problem I am facing is that when I load the image, it shows up blank in the GridView, and when it is clicked upon, the new Activity also shows a blank image. I would greatly appreciate any help I can get! Here's the code:
The getView for the custom Adapter:
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
final ImageItem imageItem = (ImageItem) getItem(position);
LayoutInflater li = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout itemLayout = (LinearLayout) li.inflate(R.layout.image_item, parent, false);
final TextView nameView = (TextView) itemLayout.findViewById(R.id.nameView);
nameView.setText(imageItem.getName());
final ImageView imageView = (ImageView) itemLayout.findViewById(R.id.imageView);
String url = imageItem.getUrl();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
//Bitmap bitmap = BitmapFactory.decodeFile(url);
Drawable d = Drawable.createFromPath(url);
imageView.setImageDrawable(d);
imageView.setLayoutParams(new LinearLayout.LayoutParams(WIDTH, HEIGHT));
imageView.setPadding(PADDING, PADDING, PADDING, PADDING);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
return itemLayout;
} else {
return convertView;
}
}

Universal Image Loader rounded image only when scroll

I'm using Universal Image Loader in a ListView, It works perfectly the first time, but the rest of the time, the image has no rounded corners. Only if I scroll the image has rounded borders again.
This is my code:
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
context).threadPriority(Thread.NORM_PRIORITY - 2)
.denyCacheImageMultipleSizesInMemory()
.discCacheFileNameGenerator(new Md5FileNameGenerator())
.tasksProcessingOrder(QueueProcessingType.LIFO).enableLogging()
.build();
// Initialize ImageLoader with configuration.
ImageLoader.getInstance().init(config);
DisplayImageOptions options = new DisplayImageOptions.Builder()
.displayer(new RoundedBitmapDisplayer(50))
.showStubImage(R.drawable.ic_app)
.showImageForEmptyUri(R.drawable.camera)
.showImageOnFail(R.drawable.ic_error).cacheInMemory().cacheOnDisc()
.build();
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.listmessages_row, null);
}//End if
ImageView avatar = (ImageView) convertView.findViewById(R.id.ImageView_MessageRow);
ImageView avatarEmpty = (ImageView) convertView.findViewById(R.id.ImageView_PhotoProfileEmpty);
final int positionAux = position;
if (listItems.get(position).avatar.equals("no_avatar")){
avatarEmpty.setVisibility(View.VISIBLE);
avatar.setVisibility(View.GONE);
}else{
avatarEmpty.setVisibility(View.GONE);
avatar.setVisibility(View.VISIBLE);
imageLoader.displayImage(IMAGES + listItems.get(position).avatar, avatar, options);
}//End if-else
return convertView;
}//End getView
I answer myserlf, I modified the options of displayImageOtions and it works perfectly all the times. I only deleted this line: cacheInMemory()
Now my display image options are:
options = new DisplayImageOptions.Builder()
.displayer(new RoundedBitmapDisplayer(50))
.showStubImage(R.drawable.ic_app)
.showImageForEmptyUri(R.drawable.camera)
.showImageOnFail(R.drawable.ic_error)
.cacheOnDisc()
.build();

'Universal Image Loader' losing my ImageView's params (size and position)

I am trying to use 'Universal Image Loader' to load images inside of my ListView. My code looks like that:
public View getView(int position, View convertView, ViewGroup parent)
if (convertView == null) {
convertView = mInflater.inflate(R.layout.offers_list_adapter, null);
holder = new ViewHolder();
holder.mIvImage = (ImageView) convertView
.findViewById(R.id.ivImage);
holder.mTxtName = (TextView) convertView.findViewById(R.id.txtName);
holder.mTxtCategoryName = (TextView) convertView
.findViewById(R.id.txtCategoryName);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
I have one rule:
If this item in my list is special one, I have to change my ImageView's params (e.g. special item has imageView bigger than normal).
So in my code I do that:
if (holder.isSpecial) {
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, 50);
layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
holder.mIvImage.setLayoutParams(layoutParams);
}
After I change my params, I call the 'Universal Image Loader' library as usual:
imageLoader.displayImage(holder.url, holder.mIvImage);
But when the library execute in my UI thread to put the image in my ImageView, my ImageView is not at center and the size is wrong. My changes that I made because this item is special are not applied.
I have my own ImageDownloader and when I use it the ImageView load with the new params correctly (center and new size), but in many other points my own class is not so good as this library.
Anyone have any idea what might be happening?
Thanks in advance,
Cheers!
My 'Universal Image Loader' configuration:
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheInMemory().cacheOnDisc()
.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
.displayer(new RoundedBitmapDisplayer(0)).build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
mContext)
.memoryCacheExtraOptions(480, 800)
.discCacheExtraOptions(480, 800, CompressFormat.JPEG, 75)
.threadPoolSize(3)
.threadPriority(Thread.NORM_PRIORITY - 1)
.denyCacheImageMultipleSizesInMemory()
.offOutOfMemoryHandling()
.memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024))
.discCache(new UnlimitedDiscCache(file))
.discCacheFileNameGenerator(new HashCodeFileNameGenerator())
.imageDownloader(
new URLConnectionImageDownloader(5 * 1000, 20 * 1000))
.defaultDisplayImageOptions(DisplayImageOptions.createSimple())
.enableLogging().defaultDisplayImageOptions(options).build();
imageLoader.init(config);
You have copy-pasted configuration from GitHub Readme. This config (on GitHub) just shows ALL possible options, but you SHOULD NOT call all of them.
You should look into example project (UniversalImageLoaderExample) to see right way of tuning and using UIL.

Categories

Resources