Android Picasso image load issue - android

I use Picasso library for manage my listview's images. Although images download, they don't appear in imageview immediately. When i scroll page, they appear spontaneously. Relative code snippet:
Picasso.with(context).load(objects.get(position).getUrl()).placeholder(R.mipmap.ins).error(R.mipmap.aramam).into(new Target() {
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Bitmap d = getCircleBitmap(bitmap);
holder.image1.setImageBitmap(d);
}
public void onBitmapFailed(Drawable errorDrawable) {
}
public void onPrepareLoad(Drawable placeHolderDrawable) {
holder.image1.setImageBitmap(getCircleBitmap(BitmapFactory.decodeResource(context.getResources(), R.mipmap.bos_prof)));
}
});
Additional info : When i re-download listview objects , they appear spontaneously too.
Why is this happening??

In picasso problem with Target , with Target they have some issue which you can find here.

Related

Blur Image using Picasso

I'm using Picasso library in my project to load images. As working with picasso, i thought to show blur image before showing original image. I tried to do it using third party library called "wasabeef transformation" suggested on stackoverflow but couldn't succeed to show blur image before showing original image. This is how i did it.
Picasso.with(context).load(message.body)
.transform(BlurTransformation(context))
.into(photo,object:Callback{
override fun onSuccess() {
Picasso.with(context).load(message.body)
.networkPolicy(NetworkPolicy.OFFLINE)
.into(photo)
}
override fun onError() {
}
})
Edited:
I didn't get any blur image here. It just shows original image after few sec.Also I'm using Recyclerview in my activity and am loading images in my Adapter of Recyclerview. Images are loaded once i get the downloaded image URL. How can i show blur image here with or without any library by Picasso or by any other loading image library. Please tell me .
Create ImageView where you want to load image and blur it. inside your Activity write following code.
Target target = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
imageViewBackground.setImageBitmap(BlurImage.fastblur(bitmap, 1f,
BLUR_PRECENTAGE));
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
imageViewBackground.setImageResource(R.mipmap.ic_launcher);
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};

Picasso Not Loading HTTP Facebook Link (NOT DUPLICATED)

Before you set the question/issue as duplicate, please read it all first.
I know it is a known issue and there's loads of question on Stackoverflow and issues on Github but believe me I tried them all.
ISSUE
Not loading this link:
https://scontent.xx.fbcdn.net/v/t1.0-1/p200x200/13872950_1066865640060722_8272182690153279858_n.jpg?oh=66a4ff80019c1fbf79bee45d32f03468&oe=59F65F50
MY CODE
Target target = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
userPicture.setImageDrawable(FunctionUtil.roundBitmap(bitmap));
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
Resources resources = getContext().getResources();
Bitmap bitmap = BitmapFactory.decodeResource(resources, R.drawable.ic_image_content_error);
userPicture.setImageDrawable(FunctionUtil.roundBitmap(bitmap));
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
Resources resources = getContext().getResources();
Bitmap bitmap = BitmapFactory.decodeResource(resources, R.drawable.ic_image_placeholder);
userPicture.setImageDrawable(FunctionUtil.roundBitmap(bitmap));
}
};
Picasso.with(getContext()).load(me.getPicture().getUrl()).into(target);
WHAT I HAVE TRIED
NO CACHE:
Picasso.with(getContext()).load(me.getPicture().getUrl()).memoryPolicy(MemoryPolicy.NO_CACHE).networkPolicy(NetworkPolicy.NO_CACHE).into(target);
NEW DOWNLOADER:
`new Picasso.Builder(getContext()).downloader(new OkHttpDownloader(getContext())).build().load(me.getPicture().getUrl()).into(target);`
BOTH:
`new Picasso.Builder(getContext()).downloader(new OkHttpDownloader(getContext())).build().load(me.getPicture().getUrl()).memoryPolicy(MemoryPolicy.NO_CACHE).networkPolicy(NetworkPolicy.NO_CACHE).into(target);`
What am I doing wrong?
Ok so the question was answered here:
https://github.com/square/picasso/issues/1658
My mistake was creating the Target as a local method property then when Picasso took a little bit more time to load the image maybe the Garbage Collector cleaned the Target reference which was make it impossible to Picasso to load it into the Target. That is why it SOMETIMES it worked.
SOLUTION
Create Target object as a global property inside the Activity to hold its reference for as long as you use the imageView you want to load the image into. That fixed the issue. :)
Thanks guys!

Android getting resized images as files

After I do an image capture intent or gallery pick intent, I receive the selected image. This image I want to resize to certain sizes the selected image and get it as new File in order to send it to server. Is possible to achieve this thing ?? Is there any library that can give you such feature ?
you can use Picasso.
Picasso
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[0])
.resize(600, 200) // resizes the image to these dimensions (in pixel). does not respect aspect ratio
.into(imageViewResize);
and if you want the file after resize you can work with target
.into(new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
// send bitmap to server
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
onBitmapLoaded give you bitmap and this what you need to send to server

Images loading randomly into view pager with Glide and SimpleTarget

I'm using Glide to load image into a ViewPager using an PagerAdapter.
When I load the images using the following method:
Glide.with(mContext).load(mImage).placeholder(R.drawable.placeholder).into(mImageView);
Everything works fine, but now I need to get the bitmap from glide and store it in a map when it loads for future edit, so I switched this method to the following one:
Glide.with(mContext).load(mImage).asBitmap().placeholder(R.drawable.placeholder).into(new SimpleTarget<Bitmap>() {
#Override
public void onLoadStarted(Drawable placeholder) {
super.onLoadStarted(placeholder);
mImageView.setImageDrawable(placeholder);
}
#Override
public void onResourceReady(Bitmap bitmap, GlideAnimation<? super Bitmap> glideAnimation) {
if (bitmap != null) {
mImageView.setImageBitmap(bitmap);
}
mBitmapMap.put(position, bitmap);
mInterface.onImageLoaded(position, bitmap);
}
});
But the result is that the image not always shown. I think it's some how related to the fact the glide loads images async and at some point it returns when instatiateItem method already finished running.
It looks like this question is related. But the suggestions there did not help me. Has someone encountered this problem and has a solution for it?
The solution for this issue was to use another kind of target, instead of using the SimpleTarget object which I was using when I wrote the question I replaced it with the BitmapImageViewTarget object which I guess handles images asynchronously better. So the final code that I used for it is:
Glide.with(BaseApplication.getInstance()).load(newContent).asBitmap().placeholder(R.drawable.ic_action_picture).into(new BitmapImageViewTarget(mIvContent) {
#Override
public void onLoadStarted(Drawable placeholder) {
super.onLoadStarted(placeholder);
mIvContent.setImageDrawable(placeholder);
}
#Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
super.onResourceReady(resource, glideAnimation);
mBitmapMap.put(position, resource);
progressBar.setVisibility(View.INVISIBLE);
mIvContent.setImageBitmap(resource);
}
#Override
public void onLoadFailed(Exception e, Drawable errorDrawable) {
super.onLoadFailed(e, errorDrawable);
progressBar.setVisibility(View.INVISIBLE);
}
});

How to access Drawable when using Picasso?

I'm using the Picasso framework to handle image loading in my Android app.
After the image is loaded, I need to access the Drawable to apply some masking operations. The issue is that Picasso converts the Drawable to a PicassoDrawable, and a simple cast back to Drawable does not work.
This is the code I have:
Picasso.with(mContext).load(image.getPath()).into(mImageView, new Callback() {
#Override
public void onSuccess() {
Util.applyMask(imageView);
}
#Override
public void onError() {
}
});
and the Util.applyMask(ImageView) method:
public static void applyMask(ImageView imageView) {
// this is where a class cast exception happens since it's actually a PicassoDrawable and not a Drawable
Bitmap mainImage = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
// ...
}
A possible solution is given by Jake Wharton in this github issue: https://github.com/square/picasso/issues/38
To sum up, the solution is: "If you want access to the Bitmap directly then you'll need to use the Target callbacks. The PicassoDrawable is used to allow fading and the debug indicator."
I'm not exactly sure how to access the Target callback. Anyone knows how to solve this?
Thanks.
This was answered at github (https://github.com/square/picasso/issues/38):
private Target target = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
}
#Override
public void onBitmapFailed() {
}
}
private void loadBitmap() {
Picasso.with(this).load("url").into(target);
}

Categories

Resources