Picasso Not Loading HTTP Facebook Link (NOT DUPLICATED) - android

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!

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) {
}
};

Android Picasso image load issue

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.

Set Picasso image as ListView Background

I am using Picasso library to load images stored on my server to my android application.
I am using the normal code to do this.
Picasso.with(context)
.load(url)
.resize(50, 50)
.centerCrop()
.into(imageView)
but now i want to set this image as a backgroud to my listview with id = myList .
Any help would be appreciated.
Thank you. :D
You could try to override the new target() implementation to set your view.
Picasso.with(context).load(url).into(new Target() {
#Override public void onSuccess(Bitmap bitmap) {
// Set imageview bitmap here.
// Do other stuff.
}
#Override public void onError() {
}
});
Please take note that the above won't work inside a ListView unless you implement hashCode/equals in your Target.
Implement the Target class.
Pseudo code:
Picasso.with(context).load(...).into(
new Target() {
public void onLoaded(Bitmap bitmap, Picasso.LoadedFrom from){
mListView.setBackground(bitmap);
}
/* ... */
}
);
Note that this code will not compile, as I don't know the exact API's, but this will help you further.

How To download and caching bitmap using Picasso library

I am using the following method
Bitmap bitmap = Picasso.with(ListofCardsActivity.this)
.load(overLayUrl).get();
for downloading and get the image from the web url.
Does this method download the image from the url every time, even if it is downloaded already?
What I want is that once the image is downloaded, then from the next time onwards, I should get the image from the cache, no need to download.
If we have the method like the above requirement. please let me know
Does this method download the image from the url every time, even if it is downloaded already?
Not if it is cached.
The Picasso instance you get back with with() is pre-configured to have a memory cache and a disk cache.
Depending on how much you are downloading, you may run out of cache space. And I would hope that Picasso uses stuff like ETag and If-Modified-Since to re-download the image if the image has changed on the server, though I have not examined their code to see if they do, as that behavior is not documented.
Does this method download the image from the url every time, even if it is downloaded already?
Not if it is cached.
According to the documentation and the source code, Picasso doesn't cache anything when using synchronous get() method.
So here is my solution for loading image synchronously and caching it with Picasso:
File fileImage = new File("/path/to/your/image");
final Bitmap[] bmpRes = new Bitmap[1];
final Semaphore semaphore = new Semaphore(0);
Picasso.with(this).load(fileImage).priority(Picasso.Priority.HIGH).into(new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
bmpRes[0] = bitmap;
semaphore.release();
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
semaphore.release();
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
try {
semaphore.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
if(bmpRes[0] != null) {
Bitmap bmp = bmpRes[0];
//TODO: Whatever you want with the bitmap
} else {
//TODO: Failed to load the image
}

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