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.
Related
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) {
}
};
I have an image on my server and I want to display it using Picasso on my Android client.
I want to add a default image when the image is loading on Picasso so I am using Target as follows:
Picasso.with(UserActivity.this).load(imageUri.toString()).transform(new RoundedTransformation(500, 1)).into(
new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
userPic.setImageBitmap(bitmap);
}
#Override
public void onBitmapFailed(Drawable drawable) {
userPic.setImageBitmap(defaultDrawable);
}
#Override
public void onPrepareLoad(Drawable drawable) {
userPic.setImageBitmap(defaultDrawable);
}
});
I want to centerCrop() and fit() this image but it gives me an error and it tells me that I cant use them with Target. Is there anyway to use these features on Picasso? Why don't they allow these two functions with Target?
You don't need to use Target to accomplish your goal.
Side note, I am not certain that you can actually use both fit() and centerCrop() together.
See this example:
Picasso.with(context)
.load(url) // Equivalent of what ends up in onBitmapLoaded
.placeholder(R.drawable.user_placeholder) // Equivalent of what ends up in onPrepareLoad
.error(R.drawable.user_placeholder_error) // Equivalent of what ends up in onBitmapFailed
.centerCrop()
.fit()
.into(imageView);
Try this
Picasso.with(context)
.load(url)
.resize(50, 50)
.centerCrop()
.fit()
.placeholder(defaultImageLink)
.error(R.drawable.user_placeholder_error)
.transform(new RoundedTransformation(500, 1))
.into(imageView)
We can also resize the image as required by the imageview which will save memory usage if the image too large.
Callback method can be used to hide the progress bar and show some text within the image View on image load fail.
Picasso.with(context)
.load(url)
.placeholder(R.drawable.placeholder_img)
.error(R.drawable.error_img)
.resize(450, 420)
.centerCrop()
.fit()
.into(imageView, new Callback() {
#Override
public void onSuccess() {
progressBar.setVisibility(View.GONE);
}
#Override
public void onError() {
progressBar.setVisibility(View.GONE);
image_failed_text.setVisibility(View.VISIBLE);
}
});
From the UX point of view, it will be great to show the user a thumbnail first until the real image completes loading, then showing it to him, but Picasso uses only a resource file as the place holder like:
Picasso.with(context)
.load(url)
.placeholder(R.drawable.user_placeholder)
.into(imageView);
So, how can I use a thumbnail URL as the placeholder? , and if I should use Picasso twice, then how?
An issue is already opened on Picasso's github page with this request, but seems it won't be added to Picasso as per JakeWharton. So how could we do it with what's available in hand?
Thanks to raveN here & the comments on the original request on github, finally I've got a working solution:
Picasso.with(context)
.load(thumb) // thumbnail url goes here
.into(imageView, new Callback() {
#Override
public void onSuccess() {
Picasso.with(context)
.load(url) // image url goes here
.placeholder(imageView.getDrawable())
.into(imageView);
}
#Override
public void onError() {
}
});
The trick here is to get the drawable from the imageView (which is the thumbnail) after the first call & pass it as a placeholder to the second call
-- update --
I've made a blog post describing the whole scenario
You could write a simple helper which calls Picasso twice (as you mentioned).
I've not tested it, but it should go like
Picasso.with(context)
.load(thumbnailUrl)
.error(errorPlaceholderId)
.into(imageView, new Callback() {
#Override
public void onSuccess() {
// TODO Call Picasso once again here
}
#Override
public void onError() {
}
);
There are a couple of different ways to get your Picasso called twice. One method I could think of (again, not tested) is
public static void loadImageWithCallback(String url, Callback callback) {
Picasso.with(context)
.load(url)
.error(errorPlaceholderId)
.into(imageView, callback);
}
public static void loadImage(String url) {
Picasso.with(context)
.load(url)
.error(errorPlaceholderId)
.into(imageView);
}
loadImageWithCallback("http://example.com/mythumbnail.jpg", new Callback() {
#Override
public void onSuccess() {
loadImage("http://example.com/myRealImage.jpg");
}
#Override
public void onError() {
}
}
Edit: All I know is that Picasso provides this callback mechanism. I'm using it in my app to hide a ProgressBar that is displayed until the image is loaded. I'll hide it in success or error callbacks - so you'll have the option to get notified when image loading is done. Then you can simply call it again. I hope the above approach works.
I originally used AbdelHady's solution but found that the larger image is only loaded after the thumbnail is done loading so I came up with this instead.
Assuming you have a utility class in your project;
public final class U {
public static void picassoCombo(final RequestCreator thumbnail,
final RequestCreator large,
final ImageView imageView) {
Target target = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
imageView.setImageBitmap(bitmap);
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
imageView.setImageDrawable(errorDrawable);
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
thumbnail.into(imageView);
}
};
imageView.setTag(target); // To prevent target from being garbage collected
large.into(target);
}
}
Usage:
U.picassoCombo(
Picasso.with(context)
.load("http://lorempixel.com/200/100/sports/1/")
.placeholder(R.drawable.ic_image_placeholder),
Picasso.with(context)
.load("http://lorempixel.com/800/400/sports/1/")
.error(R.drawable.ic_image_broken),
imageView
);
In the above example the placeholder is set first, the thumbnail url is set next, and regardless of whether the thumbnail request is done, successful, or failed, the large image request is set once it is done. If the large image request failed, then the error drawable is set.
The only issue is that if you use setIndicatorsEnabled(true) the debug indicators don't show for the large request. As far as I can tell this seems to be by design according to this issue convo
Following is my code in my class:
#Override
public void displayImageAsync(ImageView iv, MyImageLoadingListener listener)
{
ImageLoader.getInstance().displayImage(getImageUrl(), iv, listener);
}
#Override
public void loadImageAsync()
{
ImageLoader.getInstance().loadImage(getImageUrl(), new MyImageLoadingListener()
{
#Override
public void onImageLoaded(boolean completed, View view, Bitmap bitmap)
{
if (completed)
BusProvider.getInstance().post(new ImageMatchLoadedEvent(getThis(), bitmap));
}
});
}
after calling displayImageAsync (and succcessfully displaying the image), it seems, that loadImageAsync does not use the bitmap from the displayImageAsync call... Is that intended? After calling loadImageAsync once, the bitmap is cached and everything works as suppopsed...
Am I missing something? As you can see, both calls use exactly the same url...
Use picasso which is easy to use..
ImageView view = (ImageView) convertView.findViewById(R.id.ranking_prod_pic);
Picasso.with(context).load(url).into(view); //url is image url
//you can resize image if you want
/* Picasso.with(context) .load(url) .resize(50, 50) .centerCrop() .into(view) */
http://square.github.io/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);
}