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 .
Related
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);
}
}
The Android app allows users to select a photo from their phone gallery which I save the URI to realm. I then retrieve this information and use Picasso to load it into the image view. For some reason the image is not loaded.
The URI is something like:
content://com.android.providers.media.documents/document/image%3A333180
I save it to realm using mCategory.icon = imageURI.toString() and then when I load it:
Picasso.with(getContext())
.load(Uri.parse(mCategory.icon)) // mCategory.icon is a string
.resize(200, 200)
.error(R.drawable.mountain) // default image to load
.into(viewHolder.categoryIcon);
The URI you got is a ContentProvider URI to show it with Picasso you try to do this:
File file = new File(Uri.parse(mCategory.icon));
Picasso.with(getContext())
.load(file)
.resize(200, 200)
.error(R.drawable.mountain)
.into(viewHolder.categoryIcon);
P.S: Though you can convert your ContentProveder path to Absolute path still it's better if you don't since Picasso support loading from the File..
Use setLoggingEnabled(true) in picasso to check the log if there is any error message.
Mentioned URI contains the encoded value of colon : i.e. %3A.
Either rename the source OR try Glide or some other library to serve the purpose.
This is working perfectly with new version of Picasso 2.71828
Picasso.get()
.load(imageUri)
.placeholder(R.drawable.placeholder)
.fit()
.centerCrop()
.into(viewHolder.categoryIcon);
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.
I want to know how can i use nostra13 / Android-Universal-Image-Loader for displaying Images locally i.e from drawable folder along with the Memorycache. I want to use it with ViewPager.
any help will be greatly appreciated.
To load images from assets and drawables you should take ExtendedImageDownloader from example project (this class is not a part of library yet) and also set it to configuration.
UPD: Loading local resources (from drawable, assets, content provider) works out of the box since UIL v1.8.0.
See README:
String imageUri = "assets://image.png"; // from assets
String imageUri = "drawable://" + R.drawable.image; // from drawables (only images, non-9patch)
NOTE: Use drawable:// only if you really need it! Always consider the native way to load drawables — ImageView.setImageResource(...) instead of using of ImageLoader.
Whenever More than one image load from resource dynamically (#runtime) than prefer these one:
String imgUri = "drawable://" + getResources().getIdentifier(imgName, "drawable", getActivity().getPackageName());
Here, imgName = Name of image in resource
I have an image view and a string src. I want to set the imageview source to the string src that I have, but am unable to do so beacuse the method expects an int:
imgview.setImageResource(int);
Since this method takes an int how can I accomplish my goal of using a string?
Each image has a resource-number, which is an integer. Pass this number to "setImageResource" and you should be ok.
Check this link for further information:
http://developer.android.com/guide/topics/resources/accessing-resources.html
e.g.:
imageView.setImageResource(R.drawable.myimage);
To set image cource in imageview you can use any of the following ways. First confirm your image is present in which format.
If you have image in the form of bitmap then use
imageview.setImageBitmap(bm);
If you have image in the form of drawable then use
imageview.setImageDrawable(drawable);
If you have image in your resource example if image is present in drawable folder then use
imageview.setImageResource(R.drawable.image);
If you have path of image then use
imageview.setImageURI(Uri.parse("pathofimage"));
What you are looking for is probably this:
ImageView myImageView;
myImageView = mDialog.findViewById(R.id.image_id);
String src = "imageFileName"
int drawableId = this.getResources().getIdentifier(src, "drawable", context.getPackageName())
popupImageView.setImageResource(drawableId);
Let me know if this was helpful :)