Loading big size image in Volley - android

I want to download some images from the net and show them in an ImageView .So I am using Volley.
How can I reduce the size of the images before showing them in ImageView. The original size of the images are 640 by 640 and I need to use them as an icon. How can I change their size to 64*64.
The question is not related to memory management. I just need to know how can I access the bitmap which I have downloaded from Volley.

The right way:
If you control the server you're requesting images from:
Change the actual image on the server: Create a thumbnail version for your images and use it when appropriate.
If you don't control the server:
Consider creating a service that will do this for you. Something along the lines of:
Ask your image service for an image with a given URL.
Image service checks its cache for a thumbnail version for that URL. If it exists returns it.
Image service retrieves the image, creates a thumbnail version, caches it and returns it to you.
The OK way:
Manipulate the image after it has been downloaded to the device. This isn't optimal but better than nothing. Luckily, Volley provides a built in way to achieve this: simply provide a maxWidth and maxHeight when requesting the image. Something like this (assuming imageLoader is your ImageLoader and imageListener is you listener for this request:
imageLoader.get("http://...", imageListener, maxWidth, maxHeight);
Remember, the full size image will be downloaded to the device with this solution.

Related

How to display images from URL efficently

I need to display a list of images in my application, and i get those images from an API call as an URL. Now I'm using Glide to show them, but i don't like the loading effect it makes (blank space while loading, and the the image). Is there any way to instant show those images, without any loading time and possibly without downloading them?
Since the images are stored on a remote server there is no way to bypass the downloading process, however Glide makes sure to only download the image from remote server when necessary, as the docs state
By default, Glide checks multiple layers of caches before starting a
new request for an image:
Active resources - Is this image displayed in another View right now?
Memory cache - Was this image recently loaded and still in memory?
Resource - Has this image been decoded, transformed, and written to
the disk cache before? Data - Was the data this image was obtained
from written to the disk cache before? The first two steps check to
see if the resource is in memory and if so, return the image
immediately. The second two steps check to see if the image is on disk
and return quickly, but asynchronously.
If all four steps fail to find the image, then Glide will go back to
the original source to retrieve the data (the original File, Uri, Url
etc).
To resolve this,
but i don't like the loading effect it makes (blank space while
loading, and the the image)
You can instead add a default placeholder on the ImageView until the real image is downloaded. This will display a default image while your actual image is downloading and then after download completion will replace it with the actual one.
Glide
.with(context)
.load(url)
.placeholder(R.drawable.ic_placeholder) //your default placeholder resource
.into(imageView)
you can display images from url by two ways:
Without any third party library:
URL url = new URL("URL of image here");
Bitmap bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());
imageView.setImageBitmap(bitmap);
Second way is by using Picasso

Picasso is not loading image if I use resize() function

When I load a file using picasso with resize option, it doesn't show up. I'm using Picasso.with(mContext).load(file).resize(160,213).into(holder.skinImage);
The file has dimension of 4608 X 3456. It loads up when I use Picasso without resize but it takes too long to load.
Picasso.with(mContext).load(file).into(holder.skinImage);
EDIT: I just tried loading image of size 1080 X 1920 and it worked perfectly.
Strange for me that you say that with no resizing downloading takes more time - it should take exact the same time.
Resizing with Picasso .resize() is bad if you planning to obtain that image anywhere else with no resizing, since it will be cached with some -resize-160,213 prefix and when you will try to obtain it via just URL you'll get cache miss. So why not resize by XML layout argument?
Also you can enable Http logging to see, whats going on under the hood. At least you will see, if it's not downloaded or not placed (and see the cache inside picasso instance - is it picture there or not?). If you find out the second option - try to change layout.
Try to add scaletype like this
Picasso.with(mContext).load(file).resize(160,213).centerCrop().into(holder.skinImage);

Poor quality image when restart the application

In the app there is an item add an avatar in your account, when you add it - the picture is filled to the server and displayed as expected:
Good
But if i do restart the application, then image quality is lost:
Bad
When comparing the weight and size of the image between what was covered on the original server and all is fine, the weight and size is identical. Displaying an avatar comes directly from the server. What could be the problem?
Caching is missing, use Picasso. Code to display avatars:
Picasso.with(context).load(link).into(imageview);
maybe if you call
Picasso.with (context)
.load (link)
.fit()
.into(imageView);
it works

which is best? Creating bitmap from imageview or downloading the file

we are using an app for setting wallpaper in android device, for that we are doing below steps
1) we have set of images and URLs
2) We are fetching the URL on an Imageview
so now we have to set the wallpaper, for that we need the image file, which is the best way to do it?
1) Download the file directly from URL and store it in a local storage and use it as wallpaper.
or
2) Create a bitmap from the Imageview and use it as wallpaper.
Doing the second option will reduce any quality of the image we using?
First option how to we can do it?
We have fetch the images successfully inside the application.
Always prefer to cache your image downloads so that you don't have to repeat the task. Using libraries like Picasso or Glide reduces a lot of effort is handling your images while at the same time optimizing your code.
Additionally it's best to use the original image as wallpaper rather than consuming the image view because if you have set any scale type's on your image view then your image will be cropped.
Picasso allows for hassle-free image loading in your application—often in one line of code!
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);

Details About Glide Library

I am using the Glide library and wanted to know some more details about it.Does it load the entire picture into the memory,for example if i have a 1920x1080 picture and load it onto a phone with a screen size of 640x480 does it resize and compress or load the whole thing?
Also the thumbnail feature of glide,does it just load a icon version of the image so that it can be used for something like an avatar?
1) Depending on selected diskCacheStrategy Glide saves or original image (1920x1080 in your case) or image processed separately for each of your views (for example with .override(int width, int height) method). The only optimisation which Glide makes for you is storing of image in RGB_565 format instead of system default ARGB_8888.
If you are looking for strategy to reduce trafic as well as memory consumption here is description of model with downloading of images with custom sizes:
backend requirements
android client implementation
2) Thumbnail feature - it is just an option to fill the container view with reduced copy of original image insted of showing empty container or 'progress view' while downloading final image. Here is description of it's rule from Java doc thumbnail(float f):
* Loads a resource in an identical manner to this request except with the dimensions of the target multiplied
* by the given size multiplier. If the thumbnail load completes before the fullsize load, the thumbnail will
* be shown. If the thumbnail load completes afer the fullsize load, the thumbnail will not be shown.
So it is not the proper vay for avatar styling. The usual way instead is combination of override and centerCrop options.

Categories

Resources