简体   繁体   English

Android BitmapDrawable构造函数未定义

[英]Android BitmapDrawable Constructor Undefined

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. BitmapDrawable(InputStream is)不推荐使用此构造方法。 Use BitmapDrawable(Resources, java.io.InputStream) to ensure that the drawable has correctly set its target density. 使用BitmapDrawable(Resources,java.io.InputStream)确保可绘制对象已正确设置其目标密度。

BitmapDrawable(Resources res, InputStream is) Create a drawable by decoding a bitmap from the given input stream. BitmapDrawable(Resources res,InputStream is)通过从给定的输入流解码位图来创建可绘制对象。


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. 要么这应该工作,并且我设置了错误的东西,要么我需要找到另一种从Web加载图像的方法。 Does anyone know why this code doesn't compile or suggest a better way to load the image (or both)? 有谁知道为什么此代码不编译或建议一种更好的方式来加载图像(或两者)?

That constructor was added since API Level 5, so I guess you are using an older API level thus you get that error. 该构造函数是从API级别5开始添加的,因此我想您使用的是较旧的API级别,因此会收到该错误。 Try using Android 2.1 (eclair) or newer or don't use that constructor. 尝试使用Android 2.1(eclair)或更高版本,或者不使用该构造函数。

I just tried this and it worked: 我只是尝试了一下,它起作用了:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM