Android BitmapDrawable 构造函数未定义
我正在尝试从网络加载图像。到目前为止,我的代码如下:
Resources res = getResources(); InputStream is = (InputStream) new URL(url).getContent(); BitmapDrawable bitmapDrawable = new BitmapDrawable(res, is); // Error: The constructor BitmapDrawable(Resources, InputStream) is undefined
最后一行产生错误,就好像构造函数不存在一样。但是,文档说了以下内容:
BitmapDrawable(InputStream is) 该构造函数已被弃用。使用 BitmapDrawable(Resources, java.io.InputStream) 确保可绘制对象已正确设置其目标密度。
BitmapDrawable(资源是res,InputStream是) 通过解码给定输入流中的位图来创建可绘制对象。
所以,我很茫然。要么这应该可行,但我设置错误,要么我需要找到另一种方法从网络加载图像。有谁知道为什么这段代码不能编译或建议更好的方法来加载图像(或两者)?
I'm trying to load an image from the web. The code I have so far is as follows:
Resources res = getResources(); InputStream is = (InputStream) new URL(url).getContent(); BitmapDrawable bitmapDrawable = new BitmapDrawable(res, is); // Error: The constructor BitmapDrawable(Resources, InputStream) is undefined
The last line produces an error as if the constructor doesn't exist. However, the documentation says the following:
BitmapDrawable(InputStream is)
This constructor is deprecated. Use BitmapDrawable(Resources, java.io.InputStream) to ensure that the drawable has correctly set its target density.
BitmapDrawable(Resources res, InputStream is)
Create a drawable by decoding a bitmap from the given input stream.
So, I'm at a loss. Either this should work and I've got something set up wrong or I need to find another way to load the image from the web. Does anyone know why this code doesn't compile or suggest a better way to load the image (or both)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该构造函数是从 API 级别 5 开始添加的,所以我猜您使用的是较旧的 API 级别,因此您会收到该错误。尝试使用 Android 2.1 (eclair) 或更新版本,或者不使用该构造函数。
我刚刚尝试过这个并且它有效:
That constructor was added since API Level 5, so I guess you are using an older API level thus you get that error. Try using Android 2.1 (eclair) or newer or don't use that constructor.
I just tried this and it worked: