How to convert an image from URL to Drawable
I'm having a problem converting an image to a Drawable
object. I'm converting the image using:
public Drawable LoadImageFromWebOperations(String url) {
try {
InputStream is = (InputStream)new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
System.out.println("Exc=" + e);
return null;
}
}
I'm trying to put the Drawable
in a HashMap
and insert that into a ListAdapter
, however the value of the Drawable
always is something like android.graphi开发者_运维知识库cs.drawable.BitmapDrawable@405359b0
instead of an integer and I get the message in logcat
resolveUri failed on bad bitmap uri".
this is how I put the Drawable
in the HashMap
:
map.put("cover", String.valueOf(
Main.this.LoadImageFromWebOperations("http://www.asdfasfs.com/dasfas.jpg")));
Why do you expect the drawable to be an integer? It is an object you can assign to an imageView
.
There are items in your project you can refer to with their ID, that is true, but that is something else.
R.drawable.icon
is not a Drawable
, in the same sense that R.view.your_Button
is not a Button
. You would call something like getViewFromId()
on that. If you have a function that works like this:
doSomethingWithView(R.view.id);
Then it would not work with (pseudocode ofcourse)
myView = new Button();
doSomethingWithView(myView);
So if your function works with a R.drawable.id
, it is highly unlikely that it works with a Drawable (except with overloading ofcourse).
精彩评论