位图未出现在 ImageView 中

发布于 2024-10-10 15:55:48 字数 929 浏览 1 评论 0原文

当尝试从 URL 显示 JPG 时,我在使图像显示在 ImageView 中时遇到问题。我能够打开 URLConnection,将图像拉入输入流中,将该流解码为位图。我什至可以获得位图的高度和宽度值。我将位图设置为 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();
    }

While trying to display a JPG from a URL I'm having an issue getting the image to appear in my ImageView. I'm able to open a URLConnection, pull the image down in an InputStream, decode that stream into Bitmap. I can even get values for the height and width of the bitmap. I set the bitmap to the ImageView and can still get a height for the Drawable. However, the image still does not appear in my application. Any ideas of what I could be missing? Thanks for any help.

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();
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦罢 2024-10-17 15:55:48

我无法直接回答您的问题,但我可以建议针对此需求的替代解决方案。

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

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

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

ps 我还对此缓存机制进行了一些扩展,已经在我的 github Playground 中发布了(搜索“wareninja”)

I don't have answer directly to your question, but I can suggest alternative solution for this need.

Using ImageView will cause you lots of issues, for usability.
You need to avoid screen locking, and re-dowloading image is bad from optimization perspective.

I would suggest much simpler & efficient solution; you can use WebImageView from Droid-Fu.
it works like a charm. you just pass the url of the image, and you let WebImageView handle downloading, showing progress and even caching.

here is Droid-Fu's playgorund on GitHub (search for 'droid-fu')

p.s. I also did some extensions to this caching mechanism, already released in my github playground (search for 'wareninja')

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文