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 #.#
Related
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
}
i'm using fresco to display images to my app. Right now i'm trying to apply some filters to my images but the problem is that the filter library only results Bitmap. But the draweeView.setImageBitmap is deprecated.
I also tried with a post processor like this
MeshPostprocessor meshPostprocessor = new MeshPostprocessor();
meshPostprocessor.setFilter(filters.get(0));
draweeView = (SimpleDraweeView) view.findViewById(R.id.filter_image);
ImageRequest request = ImageRequestBuilder.newBuilderWithSource(image)
.setPostprocessor(meshPostprocessor)
.setResizeOptions(new ResizeOptions(100, 100))
.build();
PipelineDraweeController controller = (PipelineDraweeController)
Fresco.newDraweeControllerBuilder()
.setImageRequest(request)
.setOldController(draweeView.getController())
.build();
draweeView.setController(controller);
and here is the PostProcessor
public static class MeshPostprocessor extends BaseRepeatedPostProcessor {
private AbstractConfig.ImageFilterInterface filter;
public void setFilter(AbstractConfig.ImageFilterInterface filter) {
this.filter = filter;
update();
}
#Override
public String getName() {
return "meshPostprocessor";
}
#Override
public void process(Bitmap bitmap) {
bitmap = filter.renderImage(bitmap);
}
}
so when I click on a filter i just run this
meshPostprocessor.setFilter(colorFilterConfig.get(position));
I tried with the debugger, the code goes through all the methods (setFilter , process etc..) but the image is not changing at all...
What am i missing?
I think you don't need a BaseRepeatedPostProcessor in your case.
A normal BasePostProcessor should be sufficient here.
However, the issue seems to be your custom filter:
#Override
public void process(Bitmap bitmap) {
bitmap = filter.renderImage(bitmap);
}
I suppose it returns a different Bitmap? This does not work in Java / for Fresco.
If your filter can do the processing in place, you can use process(Bitmap bitmap) and directly modify the given bitmap (e.g. bitmap.setPixel(...)).
If you cannot do it in place, you can override process(Bitmap destBitmap, Bitmap sourceBitmap) instead and modify destBitmap.
If your bitmap changes it's size, you can override CloseableReference<Bitmap> process(Bitmap sourceBitmap, PlatformBitmapFactory bitmapFactory). However, in this case make sure to actually use the provided bitmapFactory to create the new bitmap to be efficient.
For more information, take a look at http://frescolib.org/docs/modifying-image.html for more information or check out the JavaDoc for BasePostprocessor.
ok so the way I solved this is by adding a super call on the process like this
#Override
public void process(Bitmap dest, Bitmap source) {
Bitmap filtered = filter.renderImage(source, intensity);
super.process(dest, filtered);
}
i didn't noticed that you have to call super in order for the changes to have effect.
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 am using volley library to download network responses. I know volley download images when it is needed only. But what I want to achieve is to download all the images at a time and store in my cache memory.
So basically I want to download all the images at once and store in cache so if there will no interenet also user can see all the images.
I am able to store images in cache using my own LruBitmapCache class.And to download images I am using following method
public void downloadImage(NetworkImageView view, MyApplication application, String imageUrl, Context ctx) {
NetworkImageView image = view;
ImageLoader loader = new ImageLoader(application.getRequestQueue(), new LruBitmapCache(
LruBitmapCache.getCacheSize(ctx)));
image.setImageUrl(imageUrl, loader);
}
This is working fine in downloading the images and storing in cache. But it is only downloading images those are in my main screen.
To download all the images I tried following method
public void downloadCoverImages(){
int i = 0;
for(ImageData data: GetData._instance.getImageList()){
NetworkImageView iv = new NetworkImageView(BaseActivity.this);
iv.setLayoutParams(new LinearLayout.LayoutParams(300,150));
GetData._instance.downloadImage(iv, (MyApplication) getApplicationContext(),
data.getImages().getSmallImage(),BaseActivity.this);
//Log.e("Download",++i+" "+data.getImages().getSmallImage());
}
}
But it's not to download an image a proper ImageView is required I guess and that also needs to in main screen. So in this case what should I do?
My singleton class as follows:
public enum GetData {
_instance;
private List<Sections> sectionsList = new ArrayList<Sections>();
private List<DealDetails> dealDetailsList = new ArrayList<DealDetails>();
private DealDetails dealDetails;
public List<Sections> getSectionsList() {
return sectionsList;
}
public void setSectionsList(List<Sections> sectionsList) {
this.sectionsList = sectionsList;
}
public List<DealDetails> getDealDetailsList() {
return dealDetailsList;
}
public void setDealList(List<DealDetails> dealDetailsList) {
this.dealDetailsList = dealDetailsList;
}
public DealDetails getDealDetails() {
return dealDetails;
}
public void setDealDetails(DealDetails dealDetails) {
this.dealDetails = dealDetails;
}
public void downloadImage(NetworkImageView view, MyApplication application, String imageUrl, Context ctx) {
NetworkImageView image = view;
ImageLoader loader = new ImageLoader(application.getRequestQueue(), new LruBitmapCache(
LruBitmapCache.getCacheSize(ctx)));
image.setImageUrl(imageUrl, loader);
}
}
Is it possible if I download image without using volley and store in my LRUCache and show it using volley? Any good tutorial for that? Thnaks.
you can also get bitmap of remote image using volley imageloader.. in this way you don't need to have imageview and all
imageLoader.get(data.getImages().getSmallImage(), new ImageLoader.ImageListener() {
#Override
public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
}
#Override
public void onErrorResponse(VolleyError error) {
}
});
get the imageloader instance from volley singleton class. in your case you don't need to handle the imagelistener callbacks ..
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.