How to cache images using volley - android

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.

Related

glide take time to load image even if image load from cache?

I have RecyclerView where image load from server URL. so I use Glide library to load an image, glide store image into cache to reload as fast as possible.
I use a placeholder() as a loader for loading image first time, but if I off the internet and start app its still load placeholder first and then load an image into ImageView.
So, my question is, is there any way to load an image into ImageView so user experience like smoothness (offline) app while loading images
Glide.with(context)
.load(www.abc.com/abcd.jpg)
.placeholder(R.drawable.placeholder)
.into(viewHolder.ivSleepImage)
you can check this app Walking UP it loads image first time than there is no loader, and next time when image load means when you open app image already loaded in list.
Please somebody help me to achieve this task
There are 2 ways you can do that task but it can be very lengthy because storing and loading is not the way to approach in the android development
1.Store the data in the cache
2.store data in the database
the cache strategy is relatively easy. as you are requesting the data from the server if the connection is not available then you have to load data from the cache. Same can be said from the database but it is very lengthy and can be very slow.
A quick solution to your problem would be to add diskCacheStrategy to your Glide call. This would help you cache ALL versions of your image and thus reduce load-time. Here's an implementation:
Glide.with(context)
.load(www.abc.com/abcd.jpg)
.placeholder(R.drawable.placeholder)
.into(viewHolder.ivSleepImage)
.diskCacheStrategy(DiskCacheStrategy.ALL)
I hope this helps. Merry coding!

Store images from url in cache and use them as thumbnail or placeholder like whatsapp.

I am developing story feature app like instagram or whatsapp but I got stuck somewhere. I am getting image thumbnail from server as URL but when I try to use it . It take some time to load that image.So I want to Load add thumbnail images from url in previous activity and then set that thumbnail from cache.How I can store url images in cache Hashmap or arraylist to use them in next activity.Please help.I am in trouble right now.
You can use Glide for catching the image from URL.

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

Profile Image Uploader and Load it with Picasso

I have an android app which has profile image.
The user can edit his/her profile image.
I'm using Picasso to load images, but every time I load profile image, Picasso loads old image.
When I upload profile image I don't change it's url, so profile image url for every user is constant.
First question: should I change profile image url every time user change profile image?
Second question: When user change his/her profile image I invalidate Picasso to load new image using this code:
picassoInstance.load(url)
.networkPolicy(NetworkPolicy.NO_CACHE)
.transform(new CircleTransform())
.placeholder(dr)
.into(imageView);
But Picasso show me the old one.
And one more thing:
I load image url into imageView without calling networkPolicy method at first.
How should I handle this problem?
Every Image has different name, whenever new image uploaded you have to update your image Url
Eg. current url : https://photographylife.com/nikon-d810-high-resolution-image-samples
where Image name is : nikon-d810-high-resolution-image-samples
and after uploaded new image, name will be different for example : image-sample
so you need to update that image Url : https://photographylife.com/image-sample with new name
try this, i telling you cause im doing the same hope this will help you!
So lets just discuss one by one:
Should I change profile image url every time user change profile
image?
No. You might think of other ways around. In my case, I download the picture each time the url changes with a specific name of the user (as we're considering profile picture). So when I loaded the image with Picasso I passed the file path instead of the URL so that, it won't fetch from the URL each time. Picasso doesn't do that either actually. It maintains a cache. If the file doesn't exist, just put a placeholder.
When user change his/her profile image I invalidate Picasso to load
new image using this code:
I don't see any invalidate code here in your code. I would refer to this link to see how they've solved the cache problem.
I load image url into imageView without calling networkPolicy method
at first.
So if you fetch the image from the external storage, I don't think there's a necessity to do so.
And another suggestion is to use Glide in these cases. This is almost the same thing as Picasso, but personally I like to use Glide instead of Picasso.
Hope that helps.
Using Glide instead of Picasso because Glide recommended by google
http://inthecheesefactory.com/blog/get-to-know-glide-recommended-by-google/en
Preventing for caching in Glide
Glide.with(DemoActivity.this)
.load(Uri.parse("file://" + imagePath))
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(mImage);
Add this class to the com.squareup.picasso package.
package com.squareup.picasso;
public class PicassoTools {
public static void clearCache (Picasso p) {
p.cache.clear();
}
}
Because cache has package visibility, this util class can clear the cache for you. You just have to call it:
PicassoTools.clearCache(Picasso.with(context));
I find where problem is:
I turned down disk cache but not memory cache. I should use this code:
picassoInstance.load(url)
.networkPolicy(NetworkPolicy.NO_CACHE)
.memoryPolicy(MemoryPolicy.NO_CACHE)
.transform(new CircleTransform())
.placeholder(dr)
.into(imageView);
I use this link to:
picasso-influencing-image-caching
Thanks everybody
You need to invalidate the file before loading use the code given below:
Picasso.with(getActivity()).invalidate(file);
To know more details kindly study the Picasso documentation from their website.

How to invalidate Glide cache for some specific images

I'm writing an app which needs to load a lot of images from the internet (a manga reader). I need to cache some thumbnail images for offline use, any others should be cleared when app closed.
I read some about cache invalidation on Glide page, they said the best way is to change the content url, but how Glide know if it is a modified url of old content or a new one? I'm new to Glide here.
https://github.com/bumptech/glide/wiki/Caching-and-Cache-Invalidation
Thank in advance :)
You can use following to load image:
Glide.with(context)
.signature(new StringSignature(yourVersionMetadata))
.into(imageView)
Just change yourVersionMetadata when you load image and it will not load from cache if yourVersionMetadata is different.

Categories

Resources