【问题标题】:Bitmap not appearing in ImageView位图未出现在 ImageView 中
【发布时间】:2011-01-08 09:03:37
【问题描述】:

在尝试从 URL 显示 JPG 时,我在让图像出现在我的 ImageView 中时遇到问题。我能够打开一个 URLConnection,将图像拉到 InputStream 中,将该流解码为位图。我什至可以获取位图的高度和宽度值。我将位图设置为 ImageView 并且仍然可以获得 Drawable 的高度。但是,图像仍然没有出现在我的应用程序中。关于我可能遗漏的任何想法?感谢您的帮助。

try{
    URL imgURL = new URL(imgLocation);
    URLConnection conn = imgURL.openConnection();
    conn.connect();
    InputStream is = conn.getInputStream();

    BufferedInputStream bis = new BufferedInputStream(is, 25);

    Bitmap bm = BitmapFactory.decodeStream(bis);

    if(bm != null){
        System.err.println("Image Height: " + bm.getHeight());
        System.err.println("Image Width: " + bm.getWidth());
    } else {
        System.err.println("bm is null!!!");
    }

    img.setImageBitmap(bm);
    System.err.println("Drawable Height: " + img.getDrawable().getIntrinsicHeight());

    } catch (IOException e) {
        // Print out the exception that occurred
        e.printStackTrace();
    }

【问题讨论】:

    标签: android bitmap imageview


    【解决方案1】:

    我没有直接回答您的问题,但我可以针对此需求提出替代解决方案。

    使用 ImageView 会给您带来很多可用性问题。 您需要避免屏幕锁定,从优化角度来看,重新下载图像是不好的。

    我会建议更简单有效的解决方案;您可以使用 Droid-Fu 的 WebImageView。 它就像一个魅力。您只需传递图像的 url,然后让 WebImageView 处理下载、显示进度甚至缓存。

    这是 Droid-Fu 在 GitHub 上的 playgorund(搜索“droid-fu”)

    附言我还对这个缓存机制做了一些扩展,已经在我的 github 游乐场发布(搜索“wareninja”)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-24
      相关资源
      最近更新 更多