1

我有一个listviewwith CompoundDrawable,复合中的图像drawable需要从网络加载。如何CompoundDrawable从 URL 加载图像。我认为我可以从 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 获取位图然后将位图转换为可绘制的吗?

4

1 回答 1

-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;
    }
}
于 2013-10-01T09:25:47.360 回答