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
}
Related
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)
I am tried this code, but not getting the desired output.
Picasso.with(getApplicationContext()).load(mPicList.get(position)).into(new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
zoomImageView.setImageBitmap(bitmap);
Logger.getInstance().v("qw", "ViewPagerAdapter.134.onBitmapLoaded.");
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
Logger.getInstance().v("qw", "ViewPagerAdapter.139.onBitmapFailed.");
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
Logger.getInstance().v("qw", "ViewPagerAdapter.144.onPrepareLoad.");
}
});
It always print log onPrepareLoad why????
Your problem is that nothing keeps a strong reference to the Target instance so it gets garbage collected.
You can't just call new Target() { ... } because there aren't any strong references to it. You need to store it on a field in a view holder or implement it on a subclass of a view.
See this answer:
https://stackoverflow.com/a/30681395/5476209
this guy explicitly managed garbage collection issue happening in library.
I'm not sure if there is a reason for new Target()... but you can try to use something like this:
Picasso.with(this).load(mPicList.get(position)).into(zoomImageView);
If you are in a Fragment, use getActivity() instead of this.
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.
I use mikepenz material drawer too, but I met issue about loading URL to update drawer item icon, but still failed. I can not solve it.
https://github.com/mikepenz/MaterialDrawer
please help me. Thanks
As of the latest version of the MaterialDrawer it is now recommend to use the AbstractDrawerImageLoader and overwrite the specific methods.
Using glide:
//initialize and create the image loader logic
DrawerImageLoader.init(new AbstractDrawerImageLoader() {
#Override
public void set(ImageView imageView, Uri uri, Drawable placeholder) {
Glide.with(imageView.getContext()).load(uri).placeholder(placeholder).into(imageView);
}
#Override
public void cancel(ImageView imageView) {
Glide.clear(imageView);
}
});
Or picasso:
//initialize and create the image loader logic
DrawerImageLoader.init(new AbstractDrawerImageLoader() {
#Override
public void set(ImageView imageView, Uri uri, Drawable placeholder) {
Picasso.with(imageView.getContext()).load(uri).placeholder(placeholder).into(imageView);
}
#Override
public void cancel(ImageView imageView) {
Picasso.with(imageView.getContext()).cancelRequest(imageView);
}
});
You can find a full implementation including sample code on how to define different placeholders for different targets in the GitHub repository of the MaterialDrawer.
Here's the implementation of the CustomApplication
Fixed this issue.
The MaterialDrawer supports fetching images from URLs and setting them for the Profile icons. As the MaterialDrawer does not contain an ImageLoading library the dev can choose his own implementation
Need to implement this method in your application class.
//initialize and create the image loader logic
DrawerImageLoader.init(new DrawerImageLoader.IDrawerImageLoader() {
#Override
public void set(ImageView imageView, Uri uri, Drawable placeholder) {
Picasso.with(imageView.getContext()).load(uri).placeholder(placeholder).into(imageView);
}
#Override
public void cancel(ImageView imageView) {
Picasso.with(imageView.getContext()).cancelRequest(imageView);
}
#Override
public Drawable placeholder(Context ctx) {
return null;
}
});
Have fun #.#
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.