Android:从 URL 获取 Drawable 不适用于 .jpg,仅适用于 .png
我正在开发一个 Android 应用程序,它可以从互联网页面(特别是 xckd.com)中提取图片。我使用这种基本形式的代码可以很好地工作(见下文),
ImageView iv = new ImageView;
URL url = new URL(address);
InputStream content = (InputStream)url.getContent();
Drawable d = Drawable.createFromStream(content , "src");
iv.setImageDrawable(d)
我注意到当我查看他们的一些旧漫画时,图像不会显示(但我正确地从页面中抓取了其他信息,所以我知道网址是正确的)。我确定只有当图像是 .jpg 文件时才会出现此结果,但当图像是 .png 文件时效果很好。
我在 Google 上搜索了很多,但我不明白为什么会这样,有一个简单的修复方法吗?
I am working on and Android app that pulls a picture from an internet page (specifically xckd.com). I have it working wonderfully using code of this basic form (see below)
ImageView iv = new ImageView;
URL url = new URL(address);
InputStream content = (InputStream)url.getContent();
Drawable d = Drawable.createFromStream(content , "src");
iv.setImageDrawable(d)
I noticed when I was viewing some of their older comics the image would not show (but I was scraping the other information from the page properly so I know the url is correct). I determined that this result only occurs when the image is a .jpg file but works perfectly when it is a .png
I have Googled around plenty and I can't figure out why this is, is there a simple fix for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个已知问题 http://code.google.com/p/android /issues/detail?id=6066。使用 FlushedInputStream 可以解决这个问题。
It's a known issue http://code.google.com/p/android/issues/detail?id=6066. Using FlushedInputStream solves it.