How to preload image for a NetworkImageView of Volley? - android

I'm dynamically generate NetworkImageView for an Internet image, and it starts to fetch real file when the view is created.
Is there a way to force Volley library to cache files of some url, before I really create the view? Or do I need to use ImageRequest and handle the cached files by myself?
Thank you!

I think you use ImageLoader where you can put a string url and I want to believe it caches it, but there is also a bitmap listener (to let you know when it is done loading), so I'm not sure if this can just be added to the network call to your server

Related

get bitmap after picasso sets image to an ImageView

I'm trying to save downloaded images in shared preferences in base64 format so that the app can work offline. I've used picasso
Picasso.with(mContext).load(urls.get(position)).into(imageView);
I request the server with a specific category of images and the server responds with a json containing the urls of those images. I found picasso to do this job for me as i need to write my async task for this.The images are loaded into ImageViews of a gridview image adapter.
What I want to do is that as soon as the image is loaded through the url using this function I want that bitmap image so that I can save it in shared preferences or serialize it something so that it can be used offline.
or suggest me a better solution.
Solved it!
Picasso.with(mContext)
.load(url)
.networkPolicy(NetworkPolicy.OFFLINE)
.into(imageView);
Thankyou so much guys <3

Android - calling asynctask from asyntask

I have to build some data from a json call and populate an object in Android mobile dev.
The json requires the read of the first url to get a list of data. One of the fields is an url to an image which I need to make a second call to the web using asyncTask to retrieve as a blob and save in the object.
I have the code working to get the first url call using the asyncTask. But as I process each set of data I need to make the second asyncTask call to get the image blob.
Is this possible or maybe I am going about it wrong? Sorry no code snippets.
It is not that tough as you are thinking.Follow the below example to parse image and data from json and display them:
http://www.androidbegin.com/tutorial/android-json-parse-images-and-texts-tutorial/
Create a separate package and copy the ImageLoader.java ,MemoryCache.java ,FileCache.java Utils.java
Then you can set your image within the adapter using the image url as:
ImageLoader imageLoader= new ImageLoader(context);
imageLoader.DisplayImage("the image url", imageview);
You can then reuse the imageloader class again and again throughout your project.
Apart from this you can also use third party apis to download images.
Some of them are Picasso and Glide
Currently I am using Glide.It is very powerful and easy to implement.
As per my understanding you want to display those images or download those images received in first asyncTask. I would suggest you for both purposes to use a library like picaso or fresco.
Over thinking it. I can make 2 http request under same task. Problem solved.

Picasso: Call fetch on same URL multiple times

I want to pre-fetch images using the Picasso library. In the documentation of Picasso I find the description for fetch
public void fetch():
Asynchronously fulfills the request without a ImageView or Target. This is useful when you want to warm up the cache with an image.
Note: It is safe to invoke this method from any thread.
For into(...) the behaviour is obviously that Picasso takes the image from the cache (if still available).
But what happens when I call fetch multiple times for the same image / url? Is the image re-fetched (from the network) or does Picasso find the image in the cache?

Refreshing ArrayAdapter when backend data has changed, but item value has not

I am using an ArrayAdapter along with ListView to display some images (I use Picasso to help with image handling). The images are initially loaded from the state on the local device. The adapter has ids for the images, which the getView() method of the adapter uses to get the path. If the image is not available locally, the local state returns an URL for the image. If the URL has expired, I need to fetch this from the backend.
If the URL is not expired, I have no issues, as I can either directly provide the path to Picasso or the URL and Picasso will handle everything for me. However, when I need to fetch the URL from the backend, I need to do this asynchronously. After fetching the URL, I tried doing the following but it doesn't seem to work (my getView() will in this case use Picasso with the URL):
adapter.remove(id);
adapter.add(id);
notifyDataSetChanged() doesn't seem to do much as well.
What is the best way to handle this situation? Is there any other way than what I am trying to do with the adapters? I want to delay the URL fetch from the backend as much as possible, basically until the user needs to see the image.
UPDATE: for now, I am replacing the id with a temp_id when the URL is expired and then add the correct id again, when I have the valid URL so that the view gets refreshed.
Have you tried clearing your ArrayList of image urls with myArray.clear()? Not so sure if I get your question or how your code looks like.
But I have issues like this before. What worked for me was to repopulating the adapter completely.
This link should help.

Picasso - keeping images on disk

I'm using Picasso to download images in my app. My understanding is that it uses a http client (HttpResponseCache or OkHttpClient) to cache these images on disk.
Without knowing much about either of these libraries, is it possible to mark certain images as permanent? In other words, I would like to download an image and guarantee that it will be available offline.
Thinking about it, I couldn't really have the disk cache go over a certain size, so I guess what I really need is to remove the TTL on the image url and allow the cache to remove images in a first in first out scenario.
In that case, can I control which image will be deleted first (by using a timestamp based on accessed, rather than downloaded)?
Update
Based on the answer from this SO question:
Android + Picasso: changing URL cache expiration
So this answers the first part of the question - we can control the TTL through the server.
After talking to a colleague, he pointed out that the http client should take into account how frequently an image is accessed (in addition to the TTL). So hopefully I don't have to worry about this either.
That leaves me with one question. I know which images I don't need anymore, can I remove an image from the disk cache?
You can iterate the elements in OkHttp's disk cache, and call Iterator.remove() to get rid of the ones you don't want.
http://square.github.io/okhttp/javadoc/com/squareup/okhttp/Cache.html#urls--
You'd have to extend Picasso's default cache and create a custom Picasso instance to use it:
Extend LruCache
Override the void set(String key, Bitmap bitmap) method to do what you describe (adding timestamp, etc.). Check out the original source code here.
Make sure that the original trimToSize method is never called by set (and clearKeyUri for that matter), and write your own to check for the timestamp etc to get the behavior you describe
Create a custom Picasso instance with your custom cache like this:
Picasso picasso = new Picasso.Builder(context).memoryCache(cache).build(); Picasso.setSingletonInstance(picasso);
Where cache is an instance of your custom LruCache class
when you want to store the images on the disk you should use the okhttpdownloader
OkHttpClient client = new OkHttpClient.Builder()
.cache(new Cache(getCacheDir(), Integer.MAX_VALUE))
.build();
Picasso build = new Picasso.Builder(this)
.downloader(new OkHttp3Downloader(client))
.build();
Picasso.setSingletonInstance(build);

Categories

Resources