美文网首页
获取图片名称,设置图片,不能用R.drawable.?时

获取图片名称,设置图片,不能用R.drawable.?时

作者: 小锡兵鸥 | 来源:发表于2016-11-13 22:45 被阅读101次
  1. 不把图片放在res/drawable下,而是存放在src某个package中(如:com.drawable.resource),这种情况下的调用方法为: String path = "com/drawable/resource/imageName.png";
    InputStream is = getClassLoader().getResourceAsStream(path);
    Drawable.createFromStream(is, "src");

  2. 如果还是希望直接使用res/drawable中的图片,就需要通过下面的方法了:
    假设创建工程的时候,填写的package名字为:com.test.image
    int resID = getResources().getIdentifier("imageName", "drawable", "com.test.image");

Drawable image = getResources().getDrawable(resID);

相关文章

网友评论

      本文标题:获取图片名称,设置图片,不能用R.drawable.?时

      本文链接:https://www.haomeiwen.com/subject/vxacpttx.html