Multi ImageLoaders with Picasso library in Android - 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).

Related

Setting image from resource in NetworkImageView - 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

Android, How to download two images efficiently

I've tried lots of different ways to download images and none of them actually worked, i have managed to get something working but its not perfect.
I made a new thread for downloading the bitmaps, im not storing them on the system storage or caching them in the memory for later use. If i keep opening this activity over and over, the ram usage for this app keeps getting higher and higher, and i do not want that at all!
URL url = new URL("http://10.0.0.21:80/1.png");
bitmapOne = BitmapFactory.decodeStream(url.openStream());
URL url2 = new URL("http://10.0.0.21:80/2.png");
bitmapTwo = BitmapFactory.decodeStream(url2.openStream());
runOnUiThread(new Runnable() {
#Override
public void run() {
image.setImageBitmap(bitmapOne);
image2.setImageBitmap(bitmapTwo);
}});
Thank you all for your answers! but...
I would only like to use pure java and no third party libraries for loading images, I was looking for a way to reduce memory usage and not so much of finding another way of downloading them. Im also not sure that having two urls and loading each stream looks very professional as the client could disconnect at any given moment (lets just pretend it does) and it does not load them both which are extremely required!
Use Picasso library, everything will be a breeze:
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Picasso.with(this)
.load("http://SomeUrl/CodeFeature.jpg")
.into(imageView);
A good tutorial can be found here.
https://github.com/zetbaitsu/Compressor
Usage:
compressedImageBitmap = Compressor.getDefault(this).compressToBitmap(actualImageFile);
This library maybe helpful for you.
You can visit GitHub. search "Android", sort by: "Most Stars"
, lots of awesome library you will found.
Volley can be a good alternative too, with the ability of displaying animated gifs. This snippet illustrate the basic use, here with image cache, something that can make your app not to be memory and network too demanding:
ImageLoader.ImageCache imageCache = new BitmapLruCache();
ImageLoader imageLoader = new ImageLoader(Volley.newRequestQueue(getApplicationContext()), imageCache);
NetworkImageView myImage = (NetworkImageView) findViewById(R.id.myImageView);
myImage.setImageUrl(""+mSpeaker.getHeadshotUrl(), imageLoader);
loadBitmap("http://yourdomain.com/yourImage.jpg", myImage);
For this to work all you need is the Volley library and BitmapLruCache.java

Picasso always download from network

Hi I'm trying to understand how to use the Picasso library to cache my downloaded images, so I created a very simple app with one activity, put an ImageView on it and wrote the simplest Picasso line:
Picasso.with(this).load("http://www.estambiente.it/wp-content/uploads/2009/12/Patern_test.jpg")
.placeholder(R.drawable.holder)
.error(R.drawable.error)
.into(im);
but I wanted to see the cache indicators, so I wrote this to show them:
OkHttpClient picassoClient = new OkHttpClient();
Picasso picasso = new Picasso.Builder(this).downloader(new OkHttpDownloader(picassoClient)).build();
picasso.setIndicatorsEnabled(true);
picasso.load("http://www.estambiente.it/wp-content/uploads/2009/12/Patern_test.jpg").into(im);
this code always show the red flag (meaning the image comes from the network) and if I try to open the app while I'm not connected, the error image is shown.
what am I missing here?

Performance: use Webview to display images instead of ImageView

In my Android Activity, I want to display about 10 images from web. Normally, I have to create 10 ImageView to display each image, but ImageView may cause Out of memory exception. So I decide to use 10 WebView to display image. It worked great.
But I don't know how performance of WebView, can anybody advise me how performance of 10 WebViews and Can I use this method to display images?
WebView occupies more space in memory then image view . you won't get proper scaling in webView just by simply displaying images. Webview can be faster in popping image to Ui but its not the efficient way .
You can try Volly which has in build tool for image downloading,caching and scaling and i can insure you its quite fast also.
All you need to do is add the Volley library to your project and replace ImageView in your XML layout with com.android.volley.toolbox.NetworkImageView.
Add the following variables to your Activity class:
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
Create the objects in the onCreate() method of your Activity:
mRequestQueue = Volley.newRequestQueue(context);
mImageLoader = new ImageLoader(mRequestQueue, new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap> mCache = new LruCache<String, Bitmap>(10);
public void putBitmap(String url, Bitmap bitmap) {
mCache.put(url, bitmap);
}
public Bitmap getBitmap(String url) {
return mCache.get(url);
}
});
Then download the image the getView() method of your Adapter class:
NetworkImageView image = (NetworkImageView)view.findViewById(R.id.image);
image.setImageUrl("http://someurl.com/someimage.png",mImageLoader);
In production code, you would use a global instance of both the RequestQueue and ImageLoader classes, and your onCreate() method wouldn't be cluttered as it is in this toy example.
I wouldn't use a webview to display images, though of course it can be done. If you really want to see which way is "faster", you can try out ImageView, NetworkImageView and WebView to load a large image and get a rough time estimate with the System.nanoTime() method.
Do you display 10 images at the same time ?
If yes, the size of the image view must be relatively small, so you can use small version of your images and avoid out of memory errors (or download the real size but create in memory a small version).
If not, you should use a ListView or GridView or ViewPager or something that recycle the views. You only get a couple of them in memory at the same time and avoid out of memory errors.
Furthermore, there are excellent libraries to display download and display images, for instance Universal Image Loader will do everything in the background, resize the image to the required size, you can activate the memory cache / disk cache, etc.

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