Low quality with Glide - android

When I use Glide to put and image inside an ImageView and then I "zoom in" (with a function that I bound to my ImageView), I see that the image has a low quality.
I already read Glide documentation but I'm not able to solve the problem.
Note: I use Glide inside a ViewPager.
This is what I did following the documentation
Glide
.with(context)
.loadFromMediaStore(uri)
.dontAnimate()
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.into(imageView);
This is the Manifest.xml
<manifest ...>
<application...>
<meta-data android:name="come.package.glide.MyGlideConfiguration"
android:value="GlideModule"/>
</application>
</manifest >
This is my GlideConfiguration
public class GlideConfiguration implements GlideModule {
#Override
public void applyOptions(Context context, GlideBuilder builder) {
// Apply options to the builder here.
builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);
}
#Override
public void registerComponents(Context context, Glide glide) {
// register ModelLoaders here.
}
}
I also tried doing just
Glide
.with(context)
.loadFromMediaStore(uri).asBitmap().format(DecodeFormat.ALWAYS_ARGB_8888)
But the quality is always low.
Does anybody see where is my error??

Instead of calling
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
try
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)

Related

Android studio glide issue

I have this line of code on my android studio app
if (obj.getPhotoUrl() != null) {
ImageUtil.loadImage(GlideApp.with(context), obj.getPhotoUrl(), avatarImageView);
}
But it shows error "
Required type:
GlideRequests
Provided:
com.devlomi.fireapp.utils.glide.GlideRequests "
This is my MyAppGLideModule class
#GlideModule
public class MyAppGlideModule extends AppGlideModule {
#Override
public void registerComponents(Context context, Glide glide, Registry registry) {
registry.prepend(String.class, ByteBuffer.class, new Base64ModelLoaderFactory());
}
}
How to correct the error I want to load images using the Glide
if all you want is to load an image using Glide into an ImageView, then you don't need to extend any class inorder to use Glide.
you can just load the the image using its Uri with this line of code :
Glide.with(context).load(obj.getPhotoUrl()).into(avatarImageView);
if that's not what you meant in your question, can you explain more ?
With Glide Library it becomes a little easy to load the images from a url into an ImageView
And for this we use
Glide.with(getApplicationContext).load.(obj.getPhotoUrl()).into(imageView)

android.graphics.drawable.TransitionDrawable cannot be cast to com.bumptech.glide.load.resource.bitmap.GlideBitmapDrawable

I have loaded remote image to ImageView using below code
Glide.with(context)
.load(imageUrl)
.placeholder(R.drawable.placeholderImage)
.into(holder.wallPaperImageView);
Now I want to get the bitmap from imageview
Bitmap bitmap = ((GlideBitmapDrawable)holder.wallPaperImageView.getDrawable()).getBitmap();
but above line of code throws below exception:
java.lang.ClassCastException:
android.graphics.drawable.TransitionDrawable cannot be cast to
com.bumptech.glide.load.resource.bitmap.GlideBitmapDrawable
How to resolve this?
Use this instead of that:
Bitmap bmp = ((GlideBitmapDrawable)holder.wallPaperImageView.getDrawable().getCurrent()).getBitmap();
or
Glide
.with(this)
.load(a.getAlbum_artwork())
.asBitmap()
.into(new SimpleTarget<Bitmap>(300,300) {
#Override
public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
setBackgroundImage(resource);
}
});
If you wanna get away from resizing use this (talisman):
Glide.with(context)
.load(imageUrl)
.asBitmap() //needed for obtaining bitmap
.placeholder(R.drawable.placeholderImage)
.into(holder.wallPaperImageView);
Then you are free to get bitmap:
Bitmap bitmap = ((BitmapDrawable)holder.wallPaperImageView.getDrawable()).getBitmap();
It's working with Glide 3.8.0
Good luck,'.
If you use Glide v4
private BaseTarget target = new BaseTarget<BitmapDrawable>() {
#Override
public void onResourceReady(BitmapDrawable bitmap, Transition<? super BitmapDrawable> transition) {
//here is your integration by ex: activityReference.get().toolbar.setNavigationIcon(bitmap);
}
#Override
public void getSize(SizeReadyCallback cb) {
cb.onSizeReady(100, 100);
}
#Override
public void removeCallback(SizeReadyCallback cb) {}
};
and after
Glide.with(activityReference.get().getApplicationContext())
.load(url of image)
.apply(new RequestOptions()
.placeholder(R.mipmap.img)
.error(R.mipmap.img)
.diskCacheStrategy(DiskCacheStrategy.ALL))
.apply(circleCropTransform())
.into(target);
if you have warning 'Unchecked method ...' add before method #SuppressWarnings("unchecked")
1 step
Glide.with(c2).load(applicationcalass.list.get(s1).getImguri()).asBitmap().into(t1);
2 step
Bitmap bitmap1=((BitmapDrawable) t1.getDrawable()).getBitmap();

Bad quality of image via Glide

I need to download image from url and then place it as background of my RelativeLayout.
The image (jpeg) size is about 10kb and it has good quality and resolution. But when I try to download the image with Glide it looks compressed (see background image on the screen below).
Glide.with(this)
.load("url...")
.asBitmap()
.format(DecodeFormat.PREFER_ARGB_8888)
.into(new SimpleTarget<Bitmap>(rootViewWidth, rootViewHeight) {
#Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
Drawable drawable = new BitmapDrawable(resource);
rootView.setBackground(drawable);
}
});
How can I improve that?
Thank you!
Glide default Bitmap Format is set to RGB_565 since it consumed just 50% memory footprint But picaso's Bitmap Format is ARGB_8888..
you can switch Bitmap Format to ARGB_8888 by creating a new class which extended from GlideModule like this:
public class GlideConfiguration implements GlideModule {
#Override
public void applyOptions(Context context, GlideBuilder builder) {
// Apply options to the builder here.
builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);
}
#Override
public void registerComponents(Context context, Glide glide) {
// register ModelLoaders here.
}
}
And then define it as meta-data inside AndroidManifest.xml
<meta-data android:name="com.inthecheesefactory.lab.glidepicasso.GlideConfiguration"
android:value="GlideModule"/>
It looks far better now!
All the best :)
if u have any doubt please refer this Article

Can I load images synchronously with Glide?

I know it isn't very practical to load bitmaps from the device storage synchronously, but I really have to do it. I haven't figured out any way to do this.
Yes is possible and is in glide documentation.
For example if you need to retrive the Bitmap synchronously you can do:
Glide V3:
Bitmap myBitmap = Glide.with(applicationContext)
.load(yourUrl)
.asBitmap()
.into(500, 500)
.get()
Glide v4:
FutureTarget<Bitmap> futureBitmap = Glide.with(applicationContext)
.asBitmap()
.load(yourURL))
.submit();
Bitmap myBitmap = futureBitmap.get();
Note: This code need to be run in the background or the app will crash.
Using interfaces to load data after image is loaded is a better option.
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.

Set background resource using Picasso

I know Picasso is an awesome library to play with images.
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
with this code i can load an image to an image view.
But is it possible to set a background resource , using Picasso ?
The Javadoc for Picasso's RequestCreator class has the following example:
public class ProfileView extends FrameLayout implements Target {
#Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom from) {
setBackgroundDrawable(new BitmapDrawable(bitmap));
}
#Override public void onBitmapFailed() {
setBackgroundResource(R.drawable.profile_error);
}
}
I just had a work around with the Picasso library, I was attempting to set the image as a background as well.
Picasso library made it very easy to do this, there is method by name "FIT()" which will do this job for you.
The one magic line from Picasso is
Picasso.with(context).load(mImageURLS.get(position))
.fit().placeholder(R.drawable.rtrt).into(mImageDownloader);
.fit() does the trick, thanks.

Categories

Resources