【问题标题】:how get Drawable from String path? [duplicate]如何从字符串路径中获取 Drawable? [复制]
【发布时间】:2018-03-31 11:54:30
【问题描述】:

我有带有可绘制图像的字符串字段,该图像位于@Drawable 文件夹中 我只有一个字符串路径!

我想问一下如何从路径中检索我的图像,因为不推荐使用 getDrawable

Uri uri=Uri.parse(drawableImage);
int imageResource = getResources().getIdentifier(drawableImage, null, getActivity().getPackageName());
weatherIcon.setImageDrawable(getResources().getDrawable(imageResource));

我该怎么办?不推荐使用 getDrawable

【问题讨论】:

  • 添加更多关于您想要获得什么的描述?

标签: android


【解决方案1】:

方法getDrawable()已弃用,替代方法是

ContextCompat.getDrawable(context, R.drawable.***);

但如果你有一个 String 的图像名称。那么这就是你必须做的:

getResources().getIdentifier("image_name_in_drawable_folder", "drawable", getActivity().getPackageName());

不要从 Uri 中放置完整路径。只需将标识符名称仅放入图像名称即可。

【讨论】:

  • 好的,我明白,但我没有 R.drawable.*** 我有 String url = "R.drawable.***";我该怎么办?
  • 我不明白你,但你的项目文件中的drawable文件夹中是否存在drawable???
  • 我试试...我用过:String drawableImage = "q"+urlImage; int imageResource = getResources().getIdentifier(drawableImage, null, getActivity().getPackageName()); weatherIcon.setImageDrawable((ContextCompat.getDrawable(getContext(), imageResource)));
  • 如果你的drawable在Drawable文件夹中,那么你应该使用R.drawable.your_drawable_name。当你说你有一个字符串路径而可绘制在你的apk中时,我不明白??
  • 我从 JSON 文件中删除图像的名称...是一个字符串,这个字符串是:01,所以我需要在前面添加一些字母并放入 .pgn 以在我的 Drawable 中检索资源图像文件夹...是的,我将所有可能的图像存储在我的 Drawable 文件夹中!我不知道该怎么做!
猜你喜欢
  • 1970-01-01
  • 2013-07-07
  • 2011-07-17
  • 2015-04-14
  • 2014-06-14
  • 2010-11-06
  • 2019-01-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多