【问题标题】:AssetManager and InputStream for ImageViewImageView 的 AssetManager 和 InputStream
【发布时间】:2014-04-11 22:07:36
【问题描述】:

这是我的函数的一部分,它将图像分配给我的 ImageView。我无法让它显示图像。我在资产/标志/“图像名称”下列出了我的文件。这段代码直接来自我的教科书,做了一些小的调整以适合我的代码。感谢您的帮助。

String nextImage = signNames.remove(0);
AssetManager assets = getAssets();
InputStream stream;
    try{
         stream = assets.open("signs/"+nextImage + ".gif");
         Drawable sign = Drawable.createFromStream(stream,nextImage +".gif" );
         signImageView.setImageDrawable(sign);
    }
    catch(IOException e){
         Log.e(TAG, "Error loading " +nextImage, e);
    }

【问题讨论】:

  • 您的 signImageView 确实是 ImageView?
  • 你必须使用电影类的帮助才能在图像视图中播放 gif 这里这个链接weavora.com/blog/2012/02/07/…
  • 是的,它是一个 ImageView。我已经尝试将它们更改为jpeg,仍然没有。我试过: int id = getResources().getIdentifier("yourpackagename:drawable/" + nextImage, null, null); signImageView.setImageResource(id);什么都没有

标签: android android-imageview


【解决方案1】:

我认为你正在尝试做的事情有一个更简单的习语。无需流式传输想要显示的图像。只需将图像放入res/drawable(或res/drawable-hdpi等)。然后简单地说:

 signImageView.setImageResource(R.drawable.myImage)

如果需要从动态字符串中获取资源:

int id = getResources().getIdentifier("yourpackagename:drawable/" + nextImage, null, null);
signImageView.setImageResource(R.drawable.myImage)

see here

这假设您的 signImageView 确实是 ImageView

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-09
    • 1970-01-01
    相关资源
    最近更新 更多