Android Picasso 从其 URI 加载可绘制资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21158425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Picasso load drawable resources from their URI
提问by Daniele Vitali
I have to show a drawable
from res
into an ImageView
. In this app, I'm using Picasso for some reasons.
我必须显示一个drawable
fromres
到 an ImageView
。在这个应用程序中,我出于某些原因使用毕加索。
In this case, I need to load the drawable
using its URI and not its id.
To do that, here is my code:
在这种情况下,我需要drawable
使用其 URI 而不是其 id加载。为此,这是我的代码:
uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"+context.getPackageName()+"/drawable/" + drawableName);
where drawableName used here are file names rather than their resource ids.
这里使用的 drawableName 是文件名而不是它们的资源 ID。
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.
我确信可绘制名称是正确的,但毕加索似乎不喜欢这个 uri。
采纳答案by Daniele Vitali
Found the answer. Unfortunately, Picasso do not allow drawable loading via URI. It is an incoming feature.
找到了答案。不幸的是,毕加索不允许通过 URI 加载可绘制。这是一个传入功能。
回答by Aegis
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.
毕加索无需 Uri 即可加载它。
回答by RheeBee
- 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.
如果您知道要加载的图像的整数值,则可以从 drawable 文件夹中延迟加载本地图像文件。
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
图像资源 ID
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")
是要绘制的可绘制对象的名称(即“ic_launcher”)
THIS CAN ALL BE DONE INSIDE FROM THE ADAPTER
这一切都可以在适配器内部完成
回答by Md Imran Choudhury
From picassov2+ here is a big modification. The new version is very helpful in order to manage image cache data. It's using Singleton Instance.
从picassov2+ 这里是一个很大的修改。新版本对于管理图像缓存数据非常有帮助。它正在使用单例实例。
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());
}
回答by Md Imran Choudhury
As mentioned in the documentation of Picasso.
如Picasso的文档中所述。
they are now supporting loading Image from URI like the following :
他们现在支持从 URI 加载图像,如下所示:
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 .
希望有帮助。