【问题标题】:How to show an image from an url in android如何在android中显示来自url的图像
【发布时间】:2011-06-04 18:09:54
【问题描述】:

我浏览了互联网,但找不到关于如何从 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>

【问题讨论】:

    标签: android imageview


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回复。您不介意提供此功能如何与问题中的源代码集成吗?将不胜感激。谢谢。
    • 您只需要调用该方法并将字符串url传递给该方法即可。它将返回一个可绘制对象。现在你不能用 setBackgroundDrawable() 或 setImageDrawable() 来设置你的 imageView 的背景。我想这就是你想要的......
    • 您的答案中的 awagetContent() 是什么?它在 Eclipse 中没有被识别,而是提供了一个用 getContent() 替换它的选项,然后图像不会加载它总是显示一个空白屏幕
    • 重要:当在 AsyncTask 类的方法“doInBackground”中调用此答案对我有用,因为 Android 要求在单独的线程中进行 URL 访问。
    【解决方案2】:

    不要在代码中尝试位图。只需使用简单的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 访问。
    猜你喜欢
    • 2017-01-07
    • 1970-01-01
    • 1970-01-01
    • 2011-09-18
    • 2011-09-23
    • 2014-05-20
    • 1970-01-01
    • 1970-01-01
    • 2021-06-26
    相关资源
    最近更新 更多