Does Picasso apply resize options during fetch()? - android

I am referring to this post Getting Picasso to pre-fetch forthcoming images where #BillMote uses fetch() with centerCrop() and further options:
Picasso.with(getApplicationContext())
.load(url)
.resizeDimen(R.dimen.article_image_preview_width, R.dimen.article_image_preview_height)
.centerCrop()
.fetch();
Will Picasso honor such options at all, or will Picasso save the image straight to the cache so these options are ignored and should be used later when the image is pulled from the cache and viewed?
It still isn't clear to me (and after 6 months I haven't found any documentation where this was clearly defined [I admit I haven't investigated into this either]) if Picasso applies any such options before saving the file to the cache, or repeatedly every time it displays an image.
Something like centerCrop() makes no sense to me in above statement, because the size of the target ImageView isn't clear at this point. This might be different for resizeDimen(), but I stil doubt Picasso will apply the dimensions because JakeWharton once said in a post that Picasso relies on okhttp for image caching.
Can anybody shed light please.

Related

Benefits of .centerCrop()

I'm asking this question out of curiosity. Basically I'm using glide for loading images into imageView by setting the scaleType of image to centerCrop .But what I'm asking is Is it worth or good practice to use the glide's centercrop method instead of imageview's scaletype?.
I'm asking this because as far as I know glide will first download the image and then transform it into required shape .So, I don't think it will increase the downloading speed of image. Then where this method is useful?

Out of Memory issue in a gallery like application

I am trying to create an application which contains lots of Images from web services.I am using Picasso library for loading images into a gallery like fragment with RecyclerView having lazy load and passing list of URL's to slider activity with ViewPager i am dealing lot of images so after few slides it starts throwing Out of Memory(OOM) exception.I have tried with Glide its is stretching the image so i stick with Picasso.Tried a lot of methods like using large heap and allowing hardware access.Any method to identify and handle this issue would be helpful.
Picasso.with(context)
.load(image.getImageURL())
.memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
.skipMemoryCache()
.placeholder(R.drawable.poster_default)
.error(R.drawable.poster_default)
.into(mImageView);
Agree with previous answer. You can also try use RGB_565 config. The image with this config two times smaller
Picasso picasso = new Picasso.Builder(this)
.defaultBitmapConfig(Bitmap.Config.RGB_565)
.build();
And set this instance to picasso
Picasso.setSingletonInstance(picasso);
The code snippet should be placed in onCreate method in android.app.Application class
Android uses bitmaps for images so that would mean adding large images will consume tons of memory. What I do in my projects is add fit() to the Picasso call to it so it will resize the image for me instead of loading it full size in memory.
Try something like below and see if it's fixed
Picasso.with(context)
.load(image.getImageURL())
.fit()
.centerCrop()
.memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
.skipMemoryCache()
.placeholder(R.drawable.poster_default)
.error(R.drawable.poster_default)
.into(mImageView);

How to check is Image is already cached in android mobile using Glide

I am storing my image and its reduced size image (blurred) in my Amazon Server, and store both path in database.
Now I want to know how to show blurred image first if original image is not cached and on clicking download it will download original Image. I am using Glide here...
I tried this
Glide.with(this)
.load(mainUrl)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.thumbnail(Glide.with(this)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.SOURCE))
.centerCrop()
.into(imageView);
but problem is It automatically download original image in background.
I asked in Glide Github.
https://github.com/bumptech/glide/issues/2051
So add apply(RequestOptions.onlyRetrieveFromCache()) to your RequestOptions. You can register a listener and onFailed gets called when no image is in the cache.
Glide.with(TheActivity.this)
.load("http://sampleurl.com/sample.gif")
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.into(theImageView);
Your code will prevent Glide from downloading the GIF and will only show the GIF if it is already cached, which it sounds like you don't want.
Yes, the old image will eventually be removed. By default Glide uses an LRU cache, so when the cache is full, the least recently used image will be removed. You can easily customize the size of the cache to help this along if you want. See the Configuration wiki page for how to change the cache size.
Unfortunately there isn't any way to influence the contents of the cache directly. You cannot either remove an item explicitly, or force one to be kept. In practice with an appropriate disk cache size you usually don't need to worry about doing either. If you display your image often enough, it won't be evicted. If you try to cache additional items and run out of space in the cache, older items will be evicted automatically to make space.
If you want to show placeholder before image is loading then use code below:-
Glide.with(this)
.load(mainUrl)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.placeholder("provide placeholder image here")
.thumbnail(Glide.with(this)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.SOURCE))
.centerCrop()
.into(imageView);

Pixelated images on Picasso, not sure of the cause

We are using Picasso for image handling on our app and we have been having problems with pixelated images. Unfortunately I have been unable to figure out why this is happening, whether by trying out different combinations of things on Picasso, or trying to find the answer online.
Here is our code for image handling currently (some parameter names changed to be easier to read):
Picasso.with(image.getContext())
.load(imageUrl)
.resize(100, 125)
.centerCrop()
.placeholder(R.drawable.image1)
.error(R.drawable.image2)
.transform(new RoundedTransformation(10, 4))
.into(image)
The RoundedTransformation function just gives the images rounded corners and the code for it is here (we did not write this code): https://gist.github.com/aprock/6213395
centerCrop is necessary to prevent the images from being stretched out when they are put into the ImageView.
Does anyone know why pixelation of images might be happening? Thanks and have a great day.

Reducing Image Size at Runtime for Android app

I am parsing a json schema which contains textual info and image urls for my android app. I want to read all the images from the schema and then show them in the gallery view of my android app.
But the problem is, the image urls contain HD pics and it takes a lot of time to load. Is there a way I can reduce the size of those images at run time and then display or can you suggest any improvement tip so that the images could load quickly from the schema?
Thanks
Try using Picasso library, it can resize your images whatever you like and bind it to ImageView. See also my answer here.
But if your images on your server are too big, it all depends on your internet connection, how fast they get to your device.
You can resize your image downloaded from url using picasso library. Also it will allow lazy loading of images and you can also set placeholder and error images in your imageview.
You can read complete documentation here: http://square.github.io/picasso/
Picasso.with(context)
.load(url)
.resize(50, 50)// resizing images
.centerCrop()
.into(imageView)

Categories

Resources