I am developing an Android app which displays full screen images to the user. Images are fetched from the server. I am using Glide to show the image. But I want to display a very small size blurred image before displaying the actual image. Once the image is cached, directly full sized image should be shown.
Image Displaying flows goes like this:
- If image is downloaded for the first time, first download a small scaled image, and then download full resolution image.
- If image has been downloaded before, directly show full scale image.
I cannot find any method in Glide library, which tell me if a file exist in cache or not.
Any idea, how this can be done.
Glide.with(context.getApplicationContext())
.load(Your Path) //passing your url to load image.
.override(18, 18) //just set override like this
.error(R.drawable.placeholder)
.listener(glide_callback)
.animate(R.anim.rotate_backward)
.centerCrop()
.into(image.score);
with Glide v4:
Glide.with(context)
.load(URL)
.thumbnail(0.1f)
.into(image_view)
Better you can get idea from this Library or Use this library.
2 ways to do Blur and Original image in Android.
1) Initially load Blur Image Server URL path and once get success bitmap caching mechanism to load Original(Large) Image Server URL path.
Point 1 is possible, I got answer for you( Ask your server team to get Blur image or Low quality image to make blur and then load large image).
APK Link
Glide.with(mContext)
.load(image.getMedium())
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
// Do something with bitmap here.
holder.thumbnail.setImageBitmap(bitmap);
Log.e("GalleryAdapter","Glide getMedium ");
Glide.with(mContext)
.load(image.getLarge())
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
// Do something with bitmap here.
holder.thumbnail.setImageBitmap(bitmap);
Log.e("GalleryAdapter","Glide getLarge ");
}
});
}
});
2) Fresco by Facebook is a new image library for Android. Here’s the official blog post introducing the library. It supports the streaming of progressive JPEG images over the network which can be really helpful to display large images over slow connections. One can see the image gradually improve in quality.
Progressive JPEGs with Facebook Fresco
Use BitmapFactory.Options.inSampleSize to make a downsampled version of the image, and upload both versions. Load the sample image (which should take less time) and when the bigger version is downloaded, switch to that. You could also use a TransitionDrawable to make a fade transition.
this is not a perfect way but it will be easiest way to make it.
make placeHolder image as blur and set it into glide. then always it will show blur image and then after load the actual image it will appear.
you can refer this code to use glide.
//crete this method into your Utils class and call this method wherever you want to use.
//you can set these placeHolder() and error() image static as well. I made it as comment inside this method, then no need to use [placeHolderUrl and errorImageUrl] parameters. remove it from this method.
public static void loadImage(final Activity context, ImageView imageView, String url, int placeHolderUrl, int errorImageUrl) {
if (context == null || context.isDestroyed()) return;
//placeHolderUrl=R.drawable.ic_user;
//errorImageUrl=R.drawable.ic_error;
Glide.with(context) //passing context
.load(getFullUrl(url)) //passing your url to load image.
.placeholder(placeHolderUrl) //this would be your default image (like default profile or logo etc). it would be loaded at initial time and it will replace with your loaded image once glide successfully load image using url.
.error(errorImageUrl)//in case of any glide exception or not able to download then this image will be appear . if you won't mention this error() then nothing to worry placeHolder image would be remain as it is.
.diskCacheStrategy(DiskCacheStrategy.ALL) //using to load into cache then second time it will load fast.
.animate(R.anim.fade_in) // when image (url) will be loaded by glide then this face in animation help to replace url image in the place of placeHolder (default) image.
.fitCenter()//this method help to fit image into center of your ImageView
.into(imageView); //pass imageView reference to appear the image.
}
glide has a direct way of doing this :
val thumbnailRequest = Glide.with(this)
.load("https://picsum.photos/50/50?image=0")
Glide.with(this)
.load("https://picsum.photos/2000/2000?image=0")
.thumbnail(thumbnailRequest)
.into(imageThumbnail)
from the blog:
Thumbnails are a dynamic placeholders, that can be loaded from the Internet. Fetched thumbnail will be displayed until the actual request is loaded and processed. If the thumbnail for some reason arrives after the original image, it will be dismissed.
You must have a small-size and a full-size image on your server. You can use Firebase Resize Image Extension if you are using Firebase as your backend. Once you have a small-size image URL and a full-size image URL you can use Glide like this:
Glide.with(context)
.load(fullSizeImageUrl)
.thumbnail(
Glide.with(context)
.load(smallSizeImageUrl)
).into(imageView)
You'll get that blurry effect as the small-size image will be blurry
According to Glide Doc:
.thumbnail()
loads and displays the resource retrieved by the given thumbnail request if it finishes before this request. Best used for loading thumbnail resources that are smaller and will be loaded more quickly than the full size resource. There are no guarantees about the order in which the requests will actually finish. However, if the thumb request completes after the full request, the thumb resource will never replace the full resource.
use BitmapFactory.Options.inSamleSize to load a downsampled version of the image. Then load the bigger image and do a fade transition using a TransitionDrawable
Hope this will Helps you ! Cheers !
Related
My Android application need load amounts image from Internet with api,those pictures so big and massive that need more Lots of bandwidth,So I want load partial(compress) image to save bandwidth.
At first,I tried to use the 'thumbnail' of Glide,it was seem work,the image is compressed but it load full image final.
Then,I tried to use the 'override' of Glide,but it only resized the picture in ImageView,The full image is downloaded too.
//The Thumbnail() can load the partial image at first,but download full picture final
Glide.with(mContext).load(item.imgUrl).thumbnail( 0.5f )
.apply(bitmapTransform(new RoundedCornersTransformation(50, 0)))
.into(myViewHolder.commodityImage);
//The override() can resize the picture inside ImageView,but it download the full picture too!
Glide.with(mContext).load(item.imgUrl)
.apply(bitmapTransform(new RoundedCornersTransformation(50, 0)).override(400,400))
.into(myViewHolder.commodityImage);
So,is there a way to download the image partially like the Glide thumbnial but do not download full picture.
Try to used this..
Glide.with(imageView)
.load(path)
.apply(RequestOptions().placeholder(R.drawable.place_holder).override(500, Target.SIZE_ORIGINAL))
.into(imageView)
As the title suggests, is there a way to load a low resolution image (maybe about half the image's original size) using Glide? I don't know if it makes sense but I basically need it so data users of my application can minimize their data consumption. I need it to be the final image that Glide loads into the view (not thumbnail).
I Suggest Server will do this
and you can also compress and resize the image when load
RequestOptions reqOptions = new RequestOptions()
.fitCenter()
.override(100, 100);
Glide.with(context)
.asBitmap()
.apply(reqOptions)
.load(imageUrl)
.into(bitmapView);
I am working on a chat app that is using Firebase realtime database and storage.
Currently I work with this approach to send and receive images:
On Send Button Click: Intent for the gallery >> uploading the image to Firebase storage >> storing the image URL in the messages child in Firebase Database.
In the adapter when the type is set to "image"in a child in messages, I retrieve the image to an ImageView with Picasso and its offline capabilities.
The problem is that high quality images are more than 1 MB. Once they are sent the app get very slow when scrolling the chat then some images disappears from the ImageView.
Is there anyway to make the image retrieving more smoothly and faster?
If you are using Glide then you can resize and scale your Image. Assuming you are using Glide 4.x, please use one of the following options:
First would be to use override(x, y) method where you resize the image to specific dimensions:
GlideApp
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[0])
.override(600, 200)
.into(imageViewResize);
Second would be to use override(x, y) together with centerCrop(). This is a cropping technique that scales the image so that it fills the requested bounds and then crops the extra.
GlideApp
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[0])
.override(600, 200)
.centerCrop()
.into(imageViewResizeCenterCrop);
The third would be to use fitCenter(), which is a cropping technique that scales the image so that both dimensions are equal to or less than the requested bounds of your ImageView. The image will be displayed completely, but might not fill the entire ImageView.
GlideApp
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[0])
.override(600, 200)
.fitCenter()
.into(imageViewResizeFitCenter);
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);
I'm trying to load an image from filesystem into an imageView with the ion library.
When I'm using the following code:
Ion.with(imageView)
.placeholder(R.drawable.placeholder_image)
.error(R.drawable.error_image)
.load(uri);
it (sometimes) results in a kind of blurry image.
When not using ion like the following the pictures appear sharply:
imageView.setImageBitmap(BitmapFactory.decodeFile(uri.toString()));
Is there a standard compression coming with ion that I can disable?
Strangely not every picture is blurry when using ion, e.g. I have two identical images, only with another name. When I load them into the imageView with ion one is blurry, one is not.
Any help or tips are appreciated!
Ion tries to load the image to fit the bounds of the ImageView. So make sure your ImageView is sized properly in your layout. Alternatively, if you use adjustViewBounds=true to indicate the ImageView is adjusted by the image contents, it won't do that. Or alternatively smartsize(false).
Try this,
Ion.with(context)
.load(url)
.withBitmap()
.placeholder(R.drawable.placeholder_image)
.error(R.drawable.error_image)
.intoImageView(imageView);