Download two images from different URL in the same activity with Picasso - android

I have an activity and I want to download two images, from two different URL's in it. Do I have to make two instances of the Picasso?
Picasso.with(context)
.load("http://i.imgur.com/DvpvklR.png").into(imageView);
And:
Picasso.with(context)
.load("http://my_photo.png").into(imageView);

Create Picasso single instance only use load method multiple by passing different-different imageview, check below code :
Picasso mPicasso = Picasso.with(context);
mPicasso.setIndicatorsEnabled(true);
mPicasso....load().into(imageView);

Related

How to preload images in one activity then load them in another activity?

I'm trying to preload list of images in one activity then start another activity and load these images into imageviews that are already preloaded in cache.
I have two questions
If I preload images in one activity, will they be available for load in another activity?
Am I doing the method with glide correctly or am I missing something?
Are there other more good practice ways to preload images?
I have tried Glide to preload images, but the images still take a long time to load in the another activity
Glide.with(requireContext())
.load(imageStory.image)
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
.preload();
Then loading them into imageview in another activity
Glide.with(this)
.load(stories[counter].image)
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
.into(binding.storyDisplayImage)
I expect that the list of images that I have preloaded in one activity will load many times faster in another activity than if I had not preloaded them
i use it like this
Glide.with(this)
.load(imageStory.image)
.into(imageView)
If someone searching for the same, there is the answer that I found
I found a workaround using another library, that is coil. For me, it seemed much simpler and a little better in all areas compared to glide.
fun preloading(uri: Uri?) { //for preloading images in cache
val request = ImageRequest.Builder(requireContext())
.data(uri)
.build()
requireContext().imageLoader.enqueue(request)
}
And to load images in other activities or fragments
binding.storyDisplayImage.load(stories[counter].image)

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.

Android - Does Glide image loading and caching library handle images Asynchronously?

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)

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?

Categories

Resources