如何在Android上显示来自URL的图像

Jig*_*iya 57 url https android image

我想在屏幕上显示图像.图像应来自URL,而不是可绘制的.

代码在这里:

<ImageView android:id="@+id/ImageView01" android:src = "http://l.yimg.com/a/i/us/we/52/21.gif"
    android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
Run Code Online (Sandbox Code Playgroud)

但它在编译时出错.

如何在Android中显示来自URL的图像?

Chi*_*rag 97

您可以直接在网上显示图像而无需下载.请检查以下功能.它会将网络上的图像显示在图像视图中.

public static Drawable LoadImageFromWebOperations(String url) {
    try {
        InputStream is = (InputStream) new URL(url).getContent();
        Drawable d = Drawable.createFromStream(is, "src name");
        return d;
    } catch (Exception e) {
        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后使用活动中的代码将图像设置为imageview.

  • 但什么是src名称? (47认同)
  • 需要Internet权限 (12认同)
  • 注意那个讨厌的`android.os.NetworkOnMainThreadException` (8认同)
  • @Eatlon和其他任何人都在想,"src name"在不使用9patch时是一个无用的变量.看看http://stackoverflow.com/questions/6122599/android-drawable-createfromstreamis-srcname-whats-the-2nd-parameter-meanin (5认同)
  • @ChiragPatel:这个链接可能会回答你:http://stackoverflow.com/a/6127377/944657 (2认同)
  • 在这种情况下,“src 名称”是什么? (2认同)

Sat*_*omu 17

我试过这个代码为我工作,直接从url获取图像

      private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
      ImageView bmImage;
      public DownloadImageTask(ImageView bmImage) {
          this.bmImage = bmImage;
      }

      protected Bitmap doInBackground(String... urls) {
          String urldisplay = urls[0];
          Bitmap mIcon11 = null;
          try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
          } catch (Exception e) {
              Log.e("Error", e.getMessage());
              e.printStackTrace();
          }
          return mIcon11;
      }

      protected void onPostExecute(Bitmap result) {
          bmImage.setImageBitmap(result);
      }
    }
Run Code Online (Sandbox Code Playgroud)

在onCreate()方法中使用

新的DownloadImageTask((ImageView)findViewById(R.id.image)).execute(" http://scoopak.com/wp-content/uploads/2013/06/free-hd-natural-wallpapers-download-for-pc .jpg ");


DzM*_*ter 15

您可以尝试这个我在另一个问题中找到的.

Android,在URL上创建一个等于ImageView图像的图像

try {
  ImageView i = (ImageView)findViewById(R.id.image);
  Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageUrl).getContent());
  i.setImageBitmap(bitmap); 
} catch (MalformedURLException e) {
  e.printStackTrace();
} catch (IOException e) {
  e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)


r3d*_*m4n 9

你可以试试Picasso,这真的很好,也很容易.不要忘记在清单中添加权限.

Picasso.with(context)
                     .load("http://ImageURL")
                     .resize(width,height)
                     .into(imageView );
Run Code Online (Sandbox Code Playgroud)

您还可以在这里查看教程: Youtube / Github


sat*_*sat 6

举个简单的例子,
http://www.helloandroid.com/tutorials/how-download-fileimage-url-your-device

您将不得不使用httpClient并下载图像(如果需要,将其缓存),

提供用于在listview中显示图像的解决方案,基本上是相同的代码(检查从url设置imageview的代码)以进行显示.

ListView中的图像延迟加载