如何在android中显示url中的图像

如何在android中显示url中的图像,android,imageview,Android,Imageview,我已经在互联网上搜索过了,但是没有找到一个关于如何从url显示图像的工作的示例 下面的代码正在崩溃,我无法找到原因。。非常感谢您的帮助。谢谢 TesteImages.java: package pac.image3; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import android.app.Activ

我已经在互联网上搜索过了,但是没有找到一个关于如何从url显示图像的工作的示例

下面的代码正在崩溃,我无法找到原因。。非常感谢您的帮助。谢谢

TesteImages.java:

package pac.image3;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;

public class TestImages extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.main);
        ImageView image = (ImageView) findViewById(R.id.test_image);

        String imageUrl = "http://www.small-world.net/_borders/small_world_logo.gif";
        try {
          ImageView i = (ImageView)findViewById(R.id.test_image);
          Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageUrl).getContent());
          i.setImageBitmap(bitmap);
          setContentView(image);
        } catch (MalformedURLException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }


    }
}
res/layout/main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />

    <ImageView 
   android:id="@+id/test_image"
   android:src="@drawable/icon"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   />

</LinearLayout>

只需使用以下方法从url绘制图像:

Drawable drawable_from_url(String url, String src_name) throws 
   java.net.MalformedURLException, java.io.IOException 
{
   return Drawable.createFromStream(((java.io.InputStream)
      new java.net.URL(url).awagetContent()), src_name);
}

只需将字符串url传递给该方法(对于src_name任意字符串),它将返回一个可绘制对象,然后使用imageview的setBackgroundDrawable()方法设置图像的背景

不要在代码中尝试位图。只需使用简单的
Drawable
image并实现如下方法。您将100%显示图像

Drawable drw =LoadImageFromWebOperations(imageUrl);
image.setImageDrawable(drw);

return  image;

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

谢谢你的回复。您是否介意提供这个函数如何和问题中的源代码集成?非常感谢。谢谢。您只需要调用此方法并将字符串url传递给此方法。它将返回一个可绘制对象。现在,您不能使用setBackgroundDrawable()或setImageDrawable()设置此可绘制图像的imageView背景。我想这就是你想要的…你的答案中的awagecontent()是什么??它在eclipse中没有被识别,而是提供了一个选项,用getContent()替换它,然后图像不会加载它总是显示一个空白屏幕重要的是:当在AsyncTask类的方法“doInBackground”中调用时,这个答案对我有效,因为Android需求在单独的线程中访问URL。重要提示:当在AsyncTask类的方法“doInBackground”中调用时,这个答案对我有效,因为Android需求在单独的线程中访问URL。