如何在安卓中显示来自URL的图片

3

我在互联网上寻找了很久,但是没有找到一个可以正常工作的示例来展示从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>
2个回答

7
只需使用以下方法从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的setBckgroundDrawable()方法来设置图像的背景。


谢谢您的回复。您介意提供一下这个函数如何与问题中的源代码集成吗?非常感谢。谢谢。 - nunos
你只需要调用这个方法并将字符串url传递给它。它会返回一个可绘制对象。现在,你可以使用**setBackgroundDrawable()或setImageDrawable()**将imageView的背景设置为这个可绘制对象。我认为这就是你想要的... - Dinesh Sharma
8
您好,"awagetContent()"在您的问题中指的是什么?在Eclipse中无法识别它,但会给出替换为“getContent()”的选项,但是图片加载不出来,总是显示一个空白屏幕。 - Fahad Abid Janjua
重要提示:此答案对我来说有效,当它被调用在AsyncTask类的“doInBackground”方法内部时,因为Android要求在单独的线程中进行URL访问。 - Jose Manuel Abarca Rodríguez

2

不要在代码中尝试使用Bitmap。只需使用简单的Drawable图像并实现其以下方法,您将以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;
        }

重要提示:此答案对我来说有效,当它被调用在AsyncTask类的“doInBackground”方法内部时,因为Android要求在单独的线程中进行URL访问。 - Jose Manuel Abarca Rodríguez

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接