我有listview
CompoundDrawable
,需要从网上加载化合物drawable
中的图片。
如何从URL加载图像到CompoundDrawable
。
我认为我可以从URL获取位图图像将其转换为Drawable,然后将其加载到CompoundDrawable中。像这样
public static Drawable drawableFromUrl(String url) throws IOException {
Bitmap x;
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.connect();
InputStream input = connection.getInputStream();
x = BitmapFactory.decodeStream(input);
return new BitmapDrawable(x);
}
有更好的方法吗?我可以直接做到这一点,而无需从URL中提取位图,然后将位图转换为drawable?
答案 0 :(得分:-1)
尝试这个
public static Drawable getDrawableFromURL(String url1) {
try {
URL url = new URL(url1);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
Drawable d = new BitmapDrawable(getResources(),myBitmap);
return d;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}