【问题标题】:how do i display an image from a certain URL in my android app? [duplicate]如何在我的 android 应用程序中显示来自某个 URL 的图像? [复制]
【发布时间】:2013-12-26 19:18:46
【问题描述】:

我正在制作一个应用程序,我希望在更改图像时保持更新。我不确定如何显示来自特定 URL 的图像。

感谢任何帮助和建议

谢谢

【问题讨论】:

标签: android image url curl


【解决方案1】:

如前所述,可能是重复的,但我仍然会使用 AsyncTask 从 URL 加载图像(在提供的链接中似乎不是这种情况)。

类似的东西:

new AsyncTask<String, Void, Drawable>() {
  @Override 
  protected Drawable doInBackground(String... params) {
    Drawable result = null;
    try {
      URL url = new URL(params[0]);
      HttpURLConnection connection = (HttpURLConnection) url.openConnection();
      connection.connect();
      InputStream input = connection.getInputStream();
      result = Drawable.createFromStream(input, "src");
    } catch (MalformedURLException muExp) {
      Log.e(TAG, "Bad URL provided!", muExp);
    } catch (IOException ioExp) {
      Log.e(TAG, "Error loading content from URL!", ioExp);
    }
    return result;
  }

  @Override
  protected void onPostExecute(Drawable result) {
    // Do something with your Drawable, in UI, here...
  }
}.execute("http://myimageurl.com/image.jpg");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-28
    • 2011-02-04
    • 1970-01-01
    • 2017-01-07
    • 1970-01-01
    • 2019-05-27
    • 2021-03-26
    • 1970-01-01
    相关资源
    最近更新 更多