【问题标题】:Load image in Compound Drawable From URL从 URL 加载 Compound Drawable 中的图像
【发布时间】:2013-10-07 09:11:12
【问题描述】:

我有一个listviewCompoundDrawable,复合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 获取位图然后将位图转换为可绘制的吗?

【问题讨论】:

  • 是的,这就是它的完成方式。不过,我会考虑使用适当的图像加载器来完成繁重的工作。通常,这些也会将您的图像缓存在磁盘上,这意味着您的应用不会在每次需要加载图像时都转到服务器。

标签: android compound-drawables


【解决方案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;
    }
}

【讨论】:

  • 它有什么不同?两者都是一样的。
猜你喜欢
  • 1970-01-01
  • 2019-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多