I'm using Fresco in a project, and I need to get the drawable resource of the image downloaded with it.
It's for the leanback BackgroundManager. It needs a drawable for change the background, but the image I'm getting is on the cloud.
I,ve seen examples with Glide, but no clue for Fresco. Any tips?
Glide.with(getActivity())
.load(uri)
.into(new SimpleTarget<GlideDrawable>(width, height) {
#Override
public void onResourceReady(GlideDrawable resource,
GlideAnimation<? super GlideDrawable>
glideAnimation) {
mBackgroundManager.setDrawable(resource);
}
});
Related
Like this image I am trying to set notification large icon as user profile thumbnail
like whatsapp or other chatting apps
I have tried
Glide.with(context)
.asBitmap()
.load(messageNotification.getLargeIcon())
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
builder.setLargeIcon(resource);
}
});
but it is not working..
Any Help?
If you set the large icon using glide..the you should also notify the NotificationManager onResourceReady(resource, transition)
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(Bitmap res, Transition<? super Bitmap> t) {
builder.setLargeIcon(res);
yourNotificationManager.notify(id, builder.build());
}
});
This is because glide uses background thread to load image..so before your image is loaded into builder... the notification manager is already notified (mainthread) with builder not having large image..
I am new in android developing and i have an issue. I try to load some images in a RecyclerView. The incoming data have the type byte[].
My first solution is converting byte[] to Bitmap and it works fine but the bitmaps stucks in the memory and i was getting an OutOfMemory exception.
The second solution is using Glide.
Glide.with(mContext)
.load(field.getImage())
.into(holder.mImageView);
But with this solution i get same images in each item.
I think the problem is in the Glide's cache because incoming arrays are different. I try to do solve this with this code:
requestOptions.skipMemoryCache(true);
requestOptions.diskCacheStrategy(DiskCacheStrategy.NONE);
Glide.with(mContext)
.applyDefaultRequestOptions(requestOptions)
.load(field.getImage())
.into(holder.mImageView);
}
But it doesn't help. What is the right way to do this?
try this use setDefaultRequestOptions(requestOptions)
Glide.with(context)
.setDefaultRequestOptions(new RequestOptions().placeholder(R.drawable.booked_circle).error(R.drawable.booked_circle))
.load(url)
.into(imageView);
you can resize image like this
Glide.with(PhotoGalleryActivity.this)
.using(new FirebaseImageLoader())
.load(storageReference)
.asBitmap()
.into(new SimpleTarget<Bitmap>(520, 520) {
#Override
public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
holder.img.setImageBitmap(resource);
}
});
For resizing image and adding (placeholder and error image) use this code:
RequestOptions requestOptions = new RequestOptions();
requestOptions.placeholder(R.drawable.app_icon);
requestOptions.error(R.drawable.ic_image_error);
Glide.with(context)
.setDefaultRequestOptions(requestOptions)
.asBitmap()
.load(newsContentModel.getImgNewsUrl())
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(Bitmap bitmap, Transition<? super Bitmap> transition) {
// requestLayout()
// Call this when something has changed which has
// invalidated the layout of this view.
myViewHolder.imgImage.requestLayout();
myViewHolder.imgImage.getLayoutParams().height = newHeight;
myViewHolder.imgImage.getLayoutParams().width = newWidth;
// Set the scale type for ImageView image scaling
myViewHolder.imgNewsImage.setScaleType(ImageView.ScaleType.FIT_XY);
myViewHolder.imgImage.setImageBitmap(bitmap);
}
});
I'm doing something like this to get bitmap synchronously and set it as image by using imageView.setImageBitmap(bitmap);
return Glide
.with(mContext)
.load(url)
.asBitmap()
.into(width, height)
.get();
I need to do it synchronously because I'm doing it inside AsyncTask's doInBackground. I need to fetch image url from the backend before loading it with glide.
Later I'm loading the same url into view directly with glide.
The problem is that image is not cached in the disk.
Any solution for this?
Use diskCacheStrategy
return Glide
.with(mContext)
.load(url)
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(width, height)
.get();
add diskCacheStrategy to your Glide, Select StrategyType as Source
You can have idea why to use SOURCE from this link
DiskCacheStrategy.NONE caches nothing, as discussed
DiskCacheStrategy.SOURCE caches only the original full-resolution image.
DiskCacheStrategy.RESULT caches only the final image, after reducing the resolution (and possibly transformations) (default behavior)
DiskCacheStrategy.ALL caches all versions of the image
try:
to get Bitmap From URL:
return Glide
.with(mContext)
.load(url)
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.into(width, height)
.get();
For loading in ImageView :
Glide
.with(mContext)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.into(imageView) // your ImageViewID here
For loading Image into ImageView after getting Bitmap:
Glide.with(context)
.load(url)
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
iv_image.setImageBitmap(resource); // you can store resource to some variable too if you want to update more than 1 images or try it like iv_image2.setImageBitmap(resource)
iv_image.requestLayout();
}
});
I have faced this issue many times the best way to overcome it is by using interfaces to load data after it is fetched inside glide CustomTarget
Create a new file called OnglideLoaded.java file like this:
import android.graphics.drawable.Drawable;
public interface OnGlideLoaded {
void onGlideLoaded(Drawable drawable);
}
Now create setter method for it like this where glide is:
public void setOnGlideLoaded(OnGlideLoaded onGlideLoaded) {
this.onGlideLoaded = onGlideLoaded;
}
Not call it inside Glide Target:
Glide.with(context)
.asBitmap()
.load(uri)
.into(new CustomTarget<Bitmap>() {
#Override
public void onResourceReady(#NonNull Bitmap resource, #Nullable Transition<? super Bitmap> transition) {
icon = resource;
if (onGlideLoaded!=null){
//This will send the image to every registered interface
onGlideLoaded.onGlideLoaded(icon);
}
}
#Override
public void onLoadCleared(#Nullable Drawable placeholder) {
}
});
Now call the interface like this where you want the image:
reference.setOnGlideLoaded(new OnGlideLoaded() {
#Override
public void onGlideLoaded(Drawable drawable) {
imageView.setImageBitmap(drawable);
}
});
Here, reference will be the object where you created the setter method.
I am trying to implement load bitmap in ImageView, but it is taking too much time to load. I have also used ImageLoader but it is only supports for url, it is not supports for bitmap, so how can I show bitmap instantly? Please help to solve it!
Show image using Image loader
For url I used this code
imgLoader.DisplayImage(mImage, R.mipmap.ic_launcher, imgProfile);
You can use any ImageLoader library like Glide, Picasso
Here is the snippet using Glide
Glide.with(mContext)
.load(your_image_url_or_resource_id)
.asBitmap()
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
// you can do something with loaded bitmap here
yourImageViewRef.setImageBitmap(resource);
}
});
If you want to load image using url try this code
Glide.with(context).load(your_image_url_or_resource_id).placeholder(R.drawable.placeholder_icon)
.error(R.drawable.error_icon).into(yourImageViewRef);
By using the Glide image loader library, you are able to be notified if the began loading has succeeded.
Glide
.with(this)
.load(uri)
.asBitmap()
.listener(mRequestListener)
.into(imageView);
It works well, because you can get the bitmap loaded by Glide on the onResourceReady callback method of the RequestListener class. Now use an error placeholder:
Glide
.with(this)
.load(uri)
.asBitmap()
.listener(mRequestListener)
.error(R.drawable.error_image)
.into(imageView);
What if I want to get the error image?
When the given image couldn't be found and my error image loads, only the onException callback method gets called and hence this, we can't get the error bitmap.
Are you looking for something like this:
Glide.with(context)
.load(uri)
.asBitmap()
.into(new SimpleTarget<Bitmap>(width, height) {
#Override
public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
// add image to the imageView here
}
#Override
public void onLoadFailed(Exception e, Drawable errorDrawable) {
// you are given the error drawable
}
});