How to display images from URL efficently - android

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

Related

How to show Glide images when ready? They are shown incorrectly if not fully downloaded

I am working on an application that has this image gallery activity where images are loaded from disk using Glide. These images are downloaded separately through DownloadManager. The problem is that these images might not have been downloaded by the time the user opens the gallery. I guess one way would be to load these images dynamically once each file gets downloaded by going through a BroadcastReceiver but I am wondering if there is an easier solution? Some of the images only shows parts of itself while others don't show at all, and in order to view the full images I have to navigate back into the gallery so that Glide can redo the loading. I would instead like the images to be displayed once they are able to load. I also tried using a placeholder but it will never be replaced by the image (I would also like to avoid using a placeholder image since I don't have any). The images are displayed in a GridView and loaded in getView method of a BaseAdapter. (Currently I also have to avoid storing the images in cache otherwise the incomplete images will be shown the next time as well). Code:
val file = File(urls[position].toString())
Glide.with(context)
.asBitmap()
.load(file)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.fitCenter()
.apply(RequestOptions())
.into(imageView)

Loading images using Firebase Storage UI - Will image invalidate automatically when changed?

I'm using Glide with Firebase Storage UI to load images directly using StorageReference. Will Glide invalidate the image automatically when the sourced image is changed with same file name?
Glide.with(context)
.load(storageRef)
.into(imageView);
I have tried but it's not invalidating.
If you are loading the image via url and the image is deleted and glide already loaded the image, it will stay showing until your reload it. Then it won't show anymore because the new image will have a different url.
However, if you are loadin it directly from the storage reference, then it should work fine because you wouldn't be generating a new url.

Glide showing ImageView background in between loading images

I've got a simple use case. I have a local image uri (content://path) that I load into an ImageView - that's step 1. After a button is pressed, the image is replaced with an image from our server - that's step 2.
My code is quite simple - or at least, I can reproduce the issue even after I simplified the code to the following:
Glide.with(imageHolder.getContext()).load(url).into(imageHolder);
The first time, this is called with a local uri (content://path), followed by a remote url (http://path.com).
Loading the local uri works just fine. The problem is that, once I initiate the load from the server (which might take a second), Glide rolls back to the ImageView's background image colour. So visually I get old image -> background colour -> new image, which is quite annoying.
Is there some sort of a hidden way in Glide to work around this?
this is because while the server image is being loaded there is a gap between removing local image and showing the new one and that gap is your problem.
one of the workarounds to this issue is to give Glide a placeholder (set your local image as the placeholder) so while Glide is loading image from server it still shows the local image and once the server image is loaded the local one goes away.
Glide.with(imageHolder.getContext())
.placeholder(YOUR_LOCAL_IMAGE_HERE)
.load(url)
.into(imageHolder);

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);

Android image view makes app slow

In my app I have a list view, which contains an image view. The image is loaded from bas64 encoded string. First the string is decoded and then converted it to bitmap and then the bitmap is loaded to the image view.
All the decoding is done in an async task and concurrency is handled according to the below documentation.
Processing Bitmaps Off the UI Thread
The problem is the app is scrolling slow, and all other async tasks are not executing after that.
any possible solutions?
You probably insterting a huge Bitmap into a tiny ImageView like assume the image is FHD 1920x1280 and your ImageView is 192x128. You should load a smaller or same size Bitmap into ImageView. I guess this is a reason of scroll lags. Also it could be that your layout is too complex and should be optimized.
As for
async tasks are not executing after that
nobody can tell you anything without seeing your code.
If you are getting image URL from server you can store if you want to in local DB. Don't store base64 in database just store the image URL's.But for image loading you can simply use libraries that are available like Glide or Picasso for image loading. Your list view is lagging as you are doing heavy operation like decoding base64 and loading that bitmap. Just give it a try, it will work. You can load image using single line code like
ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
Glide.with(this).load("http://goo.gl/gEgYUd").into(imageView);
you might need to put images into a folder "drawable-nodpi", since normaly android app will try to resize large image first, then load it, then show show the original image(by resize again), this process will cost much CPU which make your app respond slow. So just create a "drawable-nodpi" folder as the "drawable" folder, cut/paste all large images in the new folder

Categories

Resources