如何在ImageView中使用URL引用的图像?


当前回答

我写了一个类来处理这个问题,因为它似乎是我各种项目中反复出现的需求:

https://github.com/koush/UrlImageViewHelper

UrlImageViewHelper will fill an ImageView with an image that is found at a URL. The sample will do a Google Image Search and load/show the results asynchronously. UrlImageViewHelper will automatically download, save, and cache all the image urls the BitmapDrawables. Duplicate urls will not be loaded into memory twice. Bitmap memory is managed by using a weak reference hash table, so as soon as the image is no longer used by you, it will be garbage collected automatically.

其他回答

嗨,我有最简单的代码试试这个

    public class ImageFromUrlExample extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);  
            ImageView imgView =(ImageView)findViewById(R.id.ImageView01);
            Drawable drawable = LoadImageFromWebOperations("http://www.androidpeople.com/wp-content/uploads/2010/03/android.png");
            imgView.setImageDrawable(drawable);

    }

    private Drawable LoadImageFromWebOperations(String url)
    {
          try{
        InputStream is = (InputStream) new URL(url).getContent();
        Drawable d = Drawable.createFromStream(is, "src name");
        return d;
      }catch (Exception e) {
        System.out.println("Exc="+e);
        return null;
      }
    }
   }

main。xml

  <LinearLayout 
    android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
   <ImageView 
       android:id="@+id/ImageView01"
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content"/>

试试这个

这是一个迟到的回复,正如上面所建议的AsyncTask将会和谷歌一点后,我发现了一个解决这个问题的方法。

Drawable Drawable = Drawable. createfromstream ((InputStream) new URL(" URL ").getContent(), "src");

imageView.setImageDrawable(可拉的);

这是完整的功能:

public void loadMapPreview () {
    //start a background thread for networking
    new Thread(new Runnable() {
        public void run(){
            try {
                //download the drawable
                final Drawable drawable = Drawable.createFromStream((InputStream) new URL("url").getContent(), "src");
                //edit the view in the UI thread
                imageView.post(new Runnable() {
                    public void run() {
                        imageView.setImageDrawable(drawable);
                    }
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();
}

不要忘记在你的AndroidManifest.xml中添加以下权限来访问互联网。

< uses-permission android: name = " android.permission。互联网" / >

我自己也试过,还没有遇到任何问题。

要做到这一点,一个简单而干净的方法是使用开源库Prime。

Android Query可以为你处理这个问题,还有更多(比如缓存和加载进度)。

看看这里。

我认为这是最好的方法。

试试这个方法,希望能帮助你解决问题。

这里我解释了如何使用“AndroidQuery”外部库以asyncTask方式从url/server加载图像,并将加载的图像缓存到设备文件或缓存区。

从这里下载“AndroidQuery”库 复制/粘贴这个jar到项目lib文件夹,并将这个库添加到项目构建路径 现在我展示演示如何使用它。

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center">

        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/imageFromUrl"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"/>
            <ProgressBar
                android:id="@+id/pbrLoadImage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"/>

        </FrameLayout>
    </LinearLayout>

MainActivity.java

public class MainActivity extends Activity {

private AQuery aQuery;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    aQuery = new AQuery(this);
    aQuery.id(R.id.imageFromUrl).progress(R.id.pbrLoadImage).image("http://itechthereforeiam.com/wp-content/uploads/2013/11/android-gone-packing.jpg",true,true);
 }
}

Note : Here I just implemented common method to load image from url/server but you can use various types of method which can be provided by "AndroidQuery"to load your image easily.