How to load a bitmap into an image-view with Picasso - android

Yes, I am using Picasso to load a bitmap. The reason is I am decoding URIs in one part of my adapter, and loading bitmaps in another, and I read here that
You should always call Picasso, even if your URL is null. This way it knows that the image view was recycled.
So I tried this....
Bitmap bitMap;
...
Picasso.with(getContext())
.load(bitMap)
.into(imageView);
But I got this error
cannot resolve method 'load(android.graphics.Bitmap)'

You cant put Bitmap for load method of Picasso. You can use only uri , file , url path and int resource id.
If You are downloading image from url then you can do like as below code:
String url = "your_url";
Picasso.with(context).load(url)
.placeholder(R.drawable.any_drawable)
.error(R.drawable.anydrawable).into(your_imageView);
For other resource its same, only load method parameter would gets changed depending on the resource you are using.

Related

To display image in ImageView where drawable image ids stored in database column using Picasso

I am using Picasso to set image into ImageView from database where I have stored the drawable image id in the database. It is working perfectly when I store URL(eg https://loremflickr.com/g/320/240/paris)but it's not working for (eg R.drawable.team).
[database image]
//image is the database column containing image ids
Context context = imageText.getContext();
Picasso.with(context)
.load(image)
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.resize(50, 50)
.into(imageview);
You to pass the path of the image to set image.Use Glide library to set image it is easy and have more functionality than picasso
Picasso is only a library that makes rendering the image easier through the uri or url path, without picasso you have to create an asyncronous script that is quite complicated. But if you have dynamic data for your drawing path, you can do it with regular looping.
They are now supporting loading Image from URI like the following :
Picasso.get().load(R.drawable.landing_screen).into(imageView1);
Picasso.get().load("file:///android_asset/DvpvklR.png").into(imageView2);
Picasso.get().load(new File(...)).into(imageView3);
From picasso v2+ here is a big modification. The new version is very helpful for manage cash data. It's using Singleton Instance.
By the way, please don't save the drawable ID, just store the drawable name so that later when you call it use the method like the following
private void loadImage(String mImageName, ImageView mImageIcon){
int resID = mContext.getResources().getIdentifier(mImageName , "drawable", mContext.getPackageName());
if(resID!=0) {//The associated resource identifier. Returns 0 if no such resource was found. (0 is not a valid resource ID.)
mImageIcon.setImageResource(resID);
}
}

Picasso does not load image url reference, but loads "http://...."

Picasso doesnt load images if I use imageURL reference but loads when I use actual url e.g. "http://i2.cdn.turner.com/cnnnext/dam/assets/161017171526-cafe-neo-cup-super-169.jpg"
String imageURL = feedItem.getImageUrl();
Picasso.with(getContext()).load(imageURL).resize(600, 0).into(newsImage);
Log.i(LOG_TAG,"Image url is: "+imageURL);
Here is the log output from above
10-20 22:32:00.141 13274-13274/bw.co.fus.print I/NewsFeedAdapter: Image url is: "http://i2.cdn.turner.com/cnnnext/dam/assets/161017171526-cafe-neo-cup-super-169.jpg"
Picasso loads when I use this
Picasso.with(getContext()).load("http://i2.cdn.turner.com/cnnnext/dam/assets/161017171526-cafe-neo-cup-super-169.jpg").resize(600, 0).into(newsImage);
I have tried different resize options, including .fit() and without, also .centercrop(). Also thought it could be null but clearly its not.
Please double check if your feedItem#imageUrl variable doesn't have quotation marks on either side. According to your log output, it has. If you are passing URI to Picasso as a String, it should look like
http://whatever.com/...
and not
"http://whatever.com/..."
Also, use debugger to find out more about what's inside of your model for this specific time (beware, if you are using Realm, it will shown as null, here's why)

Using android square's picasso only for caching and decoding

I would like to use Picasso (https://github.com/square/picasso) for caching and bitmap decoding, the problem i'm having is that my request is for a url like for example : server.com/component/1 which gives me a proto file that i parse that contains some other information and a bytestring of the image that i decode to a bitmap.
Is there a way to use picasso for this even thought the request url is not just for an image or just use it for caching and decoding my bitmaps, I've tried using the class Target but it works only with a url of an image alone.
Thanks.
I think it's not possible, what can you do is just caching, load method has only 4 overrides for Uri, String, Bitmap, int. So when you will use Bitmap as a parameter, you will be able to cache your bitmap. Picasso was created only as an image loader, so I don't think that it has method that received all data from the server.

Image Adapter load method - use Bitmap Array?

For the below - I don not have the URL of the image! I cannot use the URL.
If I am downloading images from the internet into a Bitmap array like this:
Bitmap image = BitmapFactory.decodeStream(file.getReadStream());
//add to the Bitmap Array
images.add(image);
Can this be sent to Picasso (or any other Image loader like UIL?)
I know I can display the images in my Adapter but I am getting out of memory issues. And I thought Picasso could take care of this and the caching etc.
This will not work as it will not accept a Bitmap source. Is there any other library to do this?
Picasso.with(context).load(images.get(position)).resize(60, 60).error(R.drawable.error_icon).into(holder.iconImage);

Picasso load drawable resources from their URI

I have to show a drawable from res into an ImageView. In this app, I'm using Picasso for some reasons.
In this case, I need to load the drawable using its URI and not its id.
To do that, here is my code:
uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"+context.getPackageName()+"/drawable/" + drawableName);
where drawableName used here are file names rather than their resource ids.
Then
Picasso.with(context).load(uri).into(imageView);
I know for sure that drawable name is correct, but Picasso seems it does not like this uri.
If the images is in your drawable folder then you can just load it.
Picasso.with(context).load(R.drawable.drawableName).into(imageView);
and picasso will load it no need for an Uri.
Found the answer. Unfortunately, Picasso do not allow drawable loading via URI. It is an incoming feature.
This is if you don't want to hardcode the image that you are going
to load...
You can load local image files from your drawable folder lazily if you know the integer value of the image that you want to be loaded.
Then you can just do:
Picasso.with(getContext()).load(imageResourceId)
.error(R.drawable.ic_launcher)
.into(imageView);
Where
imageView
is the view you wish to display the image. For example:
imageView = (ImageView) convertView
.findViewById(R.id.itemImage);
And where
imageResourceId
is the integer value of the drawable. You can retrieve this integer value by:
int productImageId = resources.getIdentifier(
productImageName, "drawable", context.getPackageName());
as well as
productImageName
is the name of the drawable you want to draw (i.e. "ic_launcher")
THIS CAN ALL BE DONE INSIDE FROM THE ADAPTER
From picasso v2+ here is a big modification. The new version is very helpful in order to manage image cache data. It's using Singleton Instance.
GRADLE
implementation 'com.squareup.picasso:picasso:2.71828'
Set drawable image
Picasso.get()
.load(url)
.placeholder(R.drawable.user_placeholder)
.error(R.drawable.user_placeholder_error)
.into(imageView);
Bonus, get drawable by name:
public static int getDrawableIdFromFileName(Context context, String nameOfDrawable) {
return context.getResources().getIdentifier(nameOfDrawable, "drawable", context.getPackageName());
}
As mentioned in the documentation of Picasso .
they are now supporting loading Image from URI like the following :
load(android.net.Uri uri)
so you have to do something like the following :
Picasso.with(context).load(uri).into(imageView);
just like what you are doing already .
Hopethat helps .

Categories

Resources