Android BitmapDrawable构造函数未定义

时间:2011-02-05 02:03:35

标签: android image bitmap inputstream drawable

我正在尝试从网络上加载图片。我到目前为止的代码如下:


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是) 不推荐使用此构造函数。使用BitmapDrawable(Resources,java.io.InputStream)确保drawable已正确设置其目标密度。

BitmapDrawable(Resources res,InputStream is) 通过解码给定输入流中的位图来创建drawable。


所以,我很茫然。要么这应该工作,我有一些设置错误或我需要找到另一种方式从网络加载图像。有谁知道为什么这段代码不能编译或建议加载图像(或两者)的更好方法?

1 个答案:

答案 0 :(得分:1)

自API级别5以来添加了该构造函数,因此我猜您使用的是较旧的API级别,因此您会收到该错误。尝试使用Android 2.1(eclair)或更新版本,或者不要使用该构造函数。

我刚尝试了这个并且有效:

InputStream is = (InputStream) new URL(url).getContent();
BitmapDrawable bitmapDrawable = new BitmapDrawable(is);