Android BitmapDrawable构造函数未定义

Android BitmapDrawable构造函数未定义,android,image,bitmap,inputstream,drawable,Android,Image,Bitmap,Inputstream,Drawable,我正在尝试从web加载图像。我目前掌握的守则如下: Resources res = getResources(); InputStream is = (InputStream) new URL(url).getContent(); BitmapDrawable bitmapDrawable = new BitmapDrawable(res, is); // Error: The constructor BitmapDrawable(Resources, InputStream) is un

我正在尝试从web加载图像。我目前掌握的守则如下:

Resources res = getResources(); InputStream is = (InputStream) new URL(url).getContent(); BitmapDrawable bitmapDrawable = new BitmapDrawable(res, is); // Error: The constructor BitmapDrawable(Resources, InputStream) is undefined Resources res=getResources(); InputStream为=(InputStream)新URL(URL).getContent(); BitmapDrawable BitmapDrawable=新的BitmapDrawable(res,is); //错误:构造函数BitmapDrawable(资源,InputStream)未定义 最后一行生成一个错误,就好像构造函数不存在一样。然而,文件中说:


BitmapDrawable(InputStream为) 此构造函数已弃用。使用BitmapDrawable(参考资料,java.io.InputStream)确保drawable已正确设置其目标密度

BitmapDrawable(资源res,InputStream is) 通过对给定输入流中的位图进行解码来创建可绘制的



所以,我不知所措。要么这样就行了,我的设置有问题,要么我需要找到另一种方法从网络上加载图像。有人知道为什么这段代码没有编译,或者没有建议一种更好的加载图像的方法(或者两者都有)

该构造函数是在API级别5之后添加的,因此我猜您使用的是较旧的API级别,因此会出现该错误。尝试使用Android 2.1(eclair)或更新版本,或者不要使用该构造函数

我刚试过这个,效果很好:

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

我想就是这样。谢谢我正在使用API级别4,因此我的应用程序将在尚未升级的手机上运行。我不确定“资源分辨率”对图像的显示方式有多重要。换句话说,“确保可拉丝布正确设置其目标密度”是什么意思?如果它足够重要,我应该在API级别为5或以上的手机上使用它,那么Android程序员通常如何处理这种情况?可能是这样的?