Set background resource using Picasso - android

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.

Related

android picasso .into(new Target() causing method error

We are using the following code to load images into notification .setLargeicon parameter in our android webview app. But unfortunately .into(new Target() { displays the following warning:
Class 'Anonymous class derived from Target' must either be declared abstract or implement abstract method 'value()' in 'Target'. We are at loss as what we should do to solve this issue.
Picasso.get().load("https://mbracecloud.com/appln_enterprise/images/reconnect_feature7.jpg")
.into(new Target() {
#Override
public void onBitmapLoaded(final Bitmap bitmap, final Picasso.LoadedFrom from) {
notificationBuilder1.setLargeIcon(bitmap);
notificationManager.notify(notification_id, notificationBuilder1.build());
}
#Override
public void onBitmapFailed(final Drawable errorDrawable) {
// Do nothing
}
#Override
public void onPrepareLoad(final Drawable placeHolderDrawable) {
// Do nothing
}
});
Update:
Based on inputs given below, we put the following import:
import com.squareup.picasso.Target;
and tried to load image, but we are encountering a new error:
Class 'Anonymous class derived from Target' must either be declared abstract or implement abstract method 'onBitmapFailed(Exception, Drawable)' in 'Target'
Please help us out on this issue.
Guessing you're importing the wrong Target and specifically the annotation one. Change
import java.lang.annotation.Target;
to
import com.squareup.picasso.Target;
Based on your error onBitmapFailed(Exception, Drawable), you are overriding this method with only Drawable parameter but the Target class wants to override abstract method with Exception and Drawable parameters. So, try replacing onBitmapFailed with the implementation below:
#Override
public void onBitmapFailed(final Exception exception, final Drawable errorDrawable) {
// Do nothing
}

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)

Low quality with Glide

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)

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.

Categories

Resources