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
Related
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.
I would like to use the awesome Glide cache but not finding a way to synchronously check if an image is available and pull the bitmap if it is. I only see async methods. Am I missing something? I want the bitmap then a byte array, and have no plans to put it in an imageview. If its in the cache I want to sync obtains its byte array. Can this be done? I saw this: https://github.com/bumptech/glide/issues/495
But that just deals with setup issue for an imageview not really what I need.
I would like to know if Glide handles image loading asynchronously or do I have to use AsyncTask myself to handle the impact on the main thread?
I just need better clarification on this so that whenever I decide to load images into for arguments sake, a RecyclerView I can know for sure that Glide is just as good to use as Picasso (Where asynchronous media handling is concerned).
I do know that Picasso loads images asynchronously but does Glide?
I'm more familiar with volley but I took at look at Glide for you. It seems like there's a cache, so the answer should be yes, Glide should load your image asynchronously. You do not have to handle the impact on your main thread.
The cache will load and store your images for you. For valley, the code is as simple as:
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
// thumbnail image
NetworkImageView thumbNail = (NetworkImageView) view.findViewById(R.id.thumbnail);
thumbNail.setImageUrl(posterURL, imageLoader);
ImageLoader object probably has the cache which loads and holds the images.
Yes it does. Glide's API is the same as Picasso's one, and their implementations are also nearly the same (their difference is mainly in their cache implementation)
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?
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