Setting image from resource in NetworkImageView - Android - android

I would like to set the image of NetworkImageView from a resource.
I am currently using a configuration in which if there is a picture associated with the content, the image is downloaded from the web. If there is no image, a predefined resource is used.
final NetworkImageView image = (NetworkImageView) dialog.findViewById(R.id.image_quiz);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
if (noImage)
image.setDefaultImageResId(R.mipmap.splashh);
else {
urlImage = content.getImg();
image.setImageUrl(urlImage, imageLoader);
}
This snippet in contained in a modal and gets called several times.
If there is a sequence of content without an image, the resource is shown correctly.
If there is a sequence of content with images, the images are shown correctly.
THE PROBLEM
If the first content requires an image, the picture is downloaded from the web without any issue.
If the second content does not require an image, the previous image remains and is shown instead of the resource R.mipmap.splashh
Is there any method like setImageResource ?

Looking at the code of NetworkImageView, I saw that it sets SetImageBitmap(null) to clean the currentImage.
You can try to set it before calling setDefaultImageResId but you will let the NetworkImageView in a invalid state internally.
I would recommend to use a more powerful library, like Square's Picasso or Google's Glide

Related

To display image in ImageView where drawable image ids stored in database column using Picasso

I am using Picasso to set image into ImageView from database where I have stored the drawable image id in the database. It is working perfectly when I store URL(eg https://loremflickr.com/g/320/240/paris)but it's not working for (eg R.drawable.team).
[database image]
//image is the database column containing image ids
Context context = imageText.getContext();
Picasso.with(context)
.load(image)
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.resize(50, 50)
.into(imageview);
You to pass the path of the image to set image.Use Glide library to set image it is easy and have more functionality than picasso
Picasso is only a library that makes rendering the image easier through the uri or url path, without picasso you have to create an asyncronous script that is quite complicated. But if you have dynamic data for your drawing path, you can do it with regular looping.
They are now supporting loading Image from URI like the following :
Picasso.get().load(R.drawable.landing_screen).into(imageView1);
Picasso.get().load("file:///android_asset/DvpvklR.png").into(imageView2);
Picasso.get().load(new File(...)).into(imageView3);
From picasso v2+ here is a big modification. The new version is very helpful for manage cash data. It's using Singleton Instance.
By the way, please don't save the drawable ID, just store the drawable name so that later when you call it use the method like the following
private void loadImage(String mImageName, ImageView mImageIcon){
int resID = mContext.getResources().getIdentifier(mImageName , "drawable", mContext.getPackageName());
if(resID!=0) {//The associated resource identifier. Returns 0 if no such resource was found. (0 is not a valid resource ID.)
mImageIcon.setImageResource(resID);
}
}

How to cache images using volley

I am using Volley Library for my app. I want to cache images to memory and also use diskcaching for some of data required for further processing. The problem I am facing is, all the available links talks abt direct image url passed:
NetworkImageView imgAvatar = (NetworkImageView) findViewById(R.id.imgAvatar);
imageView.setImageUrl(url, imageLoader);
but in my case I dont have the URL of the Image. Can some one please help.

Multi ImageLoaders with Picasso library in Android

I'm using Picasso Library and i have (for example) five ImageView and i need to show them with Picassoand i know we can do like this:
ImageView footer1 = (ImageView) findViewById(R.id.img_thumbnail1);
Picasso.with(MainActivity.this).load("http://url.com/1.jpg").into(footer);
ImageView footer2 = (ImageView) findViewById(R.id.img_thumbnail2);
Picasso.with(MainActivity.this).load("http://url.com/2.jpg").into(footer);
ImageView footer3 = (ImageView) findViewById(R.id.img_thumbnail3);
Picasso.with(MainActivity.this).load("http://url.com/3.jpg").into(footer);
ImageView footer4 = (ImageView) findViewById(R.id.img_thumbnail4);
Picasso.with(MainActivity.this).load("http://url.com/4.jpg").into(footer);
ImageView footer5 = (ImageView) findViewById(R.id.img_thumbnail5);
Picasso.with(MainActivity.this).load("http://url.com/5.jpg").into(footer);
But, can we use AsyncTask for load these multiloading?
Or what is the best way for do?
Picasso in the above case is a singleton instance, meaning you aren't creating a new Picasso object each time you load an image. Also the Images are loaded intelligently on a background queue with a designated task dispatcher so doing what you are doing above is perfectly fine. Do it in a loop if you need to cut the code.
You can of course use AsyncTask, but it will not make it any faster. In contrary it will bring more code complexity and overhead.
Picasso will also automatically manage the number of images it tries to download at once depending on device connection type (3G, Wifi etc).

What's LazyList?

I can't find in any really credible source explaining what LazyList is. Anyone?
Lazy List is lazy loading of images from sd-card or from server using urls. It is like on demand loading of images.
Images can be cached to a local sd-card or your phone's memory. URL is considered the key. If the key is present in the sd-card, images get displayed from sd-card, otherwise it downloads the image from the server and caches it to a location of your choice. You can set a cache limit. You can also choose your own location to cache images. Cache can also be cleared.
Instead of the user waiting to download large images and then displaying them, lazy list loads images on demand. Since images are cached, you can display images offline.
https://github.com/thest1/LazyList. Lazy List
In your getview
imageLoader.DisplayImage(imageurl, imageview);
ImageLoader Display method
public void DisplayImage(String url, ImageView imageView) //url and imageview as parameters
{
imageViews.put(imageView, url);
Bitmap bitmap=memoryCache.get(url); //get image from cache using url as key
if(bitmap!=null) //if image exists
imageView.setImageBitmap(bitmap); //display iamge
else //downlaod image and dispaly. add to cache.
{
queuePhoto(url, imageView);
imageView.setImageResource(stub_id);
}
}
An alternative to Lazy List is Universal Image Loader
https://github.com/nostra13/Android-Universal-Image-Loader. It is based on Lazy List (it works on the same principle), but it has lot of other configurations. I would prefer to use Universal Image Loader because it gives you more configuration options. It can display an error image if a download failed. It can display images with rounded corners. It can cache on disc or memory. It can compress an image.
In your custom adapter constructor
File cacheDir = StorageUtils.getOwnCacheDirectory(a, "your folder");
// Get singletone instance of ImageLoader
imageLoader = ImageLoader.getInstance();
// Create configuration for ImageLoader (all options are optional)
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(a)
// You can pass your own memory cache implementation
.discCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own disc cache implementation
.discCacheFileNameGenerator(new HashCodeFileNameGenerator())
.enableLogging()
.build();
// Initialize ImageLoader with created configuration. Do it once.
imageLoader.init(config);
options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.stub_id)//display stub image
.cacheInMemory()
.cacheOnDisc()
.displayer(new RoundedBitmapDisplayer(20))
.build();
In your getView()
ImageView image=(ImageView)vi.findViewById(R.id.imageview);
imageLoader.displayImage(imageurl, image,options);//provide imageurl, imageview and options
You can configure Universal Image Loader with other options to suit your needs.
Along with LazyList/Universal Image Loader you can view this website for smooth scrolling and performance.
http://developer.android.com/training/improving-layouts/smooth-scrolling.html.
AFAIK, I'll explain you with the example
If you list contain lot of images with Text, it will take some time for your list to load because you need to download images and you need to populate them in the list. Suppose if your list contains 100 images It will take lot of time to download each image and to show it the listitem. To make the user wait until the images loads is not user friendly.
so What we need to do. At this point of time lazy list comes into picture. It is the idea that let the images be loaded in background and show text mean while.
Everybody know that listview recycle its views for every view. i.e if your listview contains 40 elemtns then listview won't allocate memory for 40 items instead it allocate memory for the visible items, i.e say you can see only 10 items at a time. so listview will allocate 10 items meemory.
So When ever you scroll the view, then the view will refresh. because of the you'll lose your reference to images and you need to download them agian. in order to avoid that, caching comes into picture.
This example is based on my knowledge in listview, I am not saying this is only correct. There might be wrong in the answer, if any body find feel free to inform me.
I think this is the other way around. AFAIK, Lazy Loading is the definition, where you actually load the data only when you need it, and it's a good design practice.
So I believe the same applies for this, only this time it's being referring to the List View.
If I'm wrong, please correct me.
The best example of lazy list is facebook notifications,messages,requests. when you scroll then data will be load.

Novoda ImageLoader - loaded same image

I'm using Novoda ImageLoader (https://github.com/novoda/ImageLoader) and when I use this loader in CursorAdapter everything is fine. But when I want to use without any Adapter in Activity with one ImageView, I get same image for different URLs.
//onCreate in Activity
imageManager = MyApplication.getImageLoader();
imageTagFactory = new ImageTagFactory(this, R.drawable.ic_refresh_inverse);
imgView = (ImageView) findViewById(R.id.imgIV);
//fillData - when I finish loading img URL from DB with CursorLoader
imgView.setTag(imageTagFactory.build(imgURL));
imageManager.getLoader().load(imgView);
//code in onCreate Application
LoaderSettings settings = new SettingsBuilder().withDisconnectOnEveryCall(true).withCacheManager(new LruBitmapCache(this)).build(this);
imageManager = new ImageManager(this, settings);
When I looked to the cache directory, the different image is loaded, but not used.
Any idea where can be problem or how to properly use this library to simply load only one image?
Thank you!
The issue is probably similar to this https://github.com/novoda/ImageLoader/issues/41
do you have query parameters in the url? if so try to add this
.withEnableQueryInHashGeneration(false) on the setting builder
This is a bug that is inverting the logic behind the parameter enableQueryInHashGeneration. This parameter should enable/disable (by default should be enable) the query part of the url in the generation of the hashname of the image. This is useful when you have session token in the url as a parameter and you don't want to download the same image over and over.
The problem will be solved in the 1.5.6 version

Categories

Resources