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. 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 开发者_Python百科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. Try using Android 2.1 (eclair) or newer or don't use that constructor.
I just tried this and it worked:
InputStream is = (InputStream) new URL(url).getContent();
BitmapDrawable bitmapDrawable = new BitmapDrawable(is);
精彩评论