来自url的Android ImageView

标签 android image url

我正在从 url 下载图片,但下载完成后图片没有更改。 我在下面输入代码,有人遇到过同样的情况吗?

Java 文件

public class MyImgActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ImageView imgView =(ImageView)findViewById(R.id.imageView1);
    Drawable drawable = LoadImageFromWebOperations("http://www.gophoto.it/view.php?i=http://1.bp.blogspot.com/-2LTvCCufBKc/T3L3KgcTj2I/AAAAAAAABbQ/Ki60e1LU9sE/s1600/Sachin%2BTendulkar.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;
      }
}
}

XML 文件

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

<ImageView 
    android:id="@+id/imageView1"
    android:layout_height="match_parent" 
    android:layout_width="match_parent"></ImageView>
</LinearLayout>

list 文件

<uses-permission android:name="android.permission.INTERNET"/>

最佳答案

请使用以下代码下载图像并将其显示到 imageview 中。

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

        Bitmap bitmap = DownloadImage("http://www.gophoto.it/view.php?i=http://1.bp.blogspot.com/-2LTvCCufBKc/T3L3KgcTj2I/AAAAAAAABbQ/Ki60e1LU9sE/s1600/Sachin%2BTendulkar.png");
        ImageView img = (ImageView) findViewById(R.id.img);
        img.setImageBitmap(bitmap);
    }

    private InputStream OpenHttpConnection(String urlString) throws IOException {
        InputStream in = null;
        int response = -1;

        URL url = new URL(urlString);
        URLConnection conn = url.openConnection();

        if (!(conn instanceof HttpURLConnection))
            throw new IOException("Not an HTTP connection");

        try {
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET");
            httpConn.connect();
            response = httpConn.getResponseCode();
            if (response == HttpURLConnection.HTTP_OK) {
                in = httpConn.getInputStream();
            }
        } catch (Exception ex) {
            throw new IOException("Error connecting");
        }
        return in;
    }

    private Bitmap DownloadImage(String URL) {
        Bitmap bitmap = null;
        InputStream in = null;
        try {
            in = OpenHttpConnection(URL);
            bitmap = BitmapFactory.decodeStream(in);
            in.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        return bitmap;
    }
}

关于来自url的Android ImageView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12172928/

相关文章:

ios - 不同屏幕尺寸的用户界面图像大小?

ASP.Net 按钮不会呈现背景图像

apache - 在 apache windows 上禁止访问包含冒号符号 ":"的 url

android -- 将部分线性布局隐藏在屏幕外的右侧

image - 如何自动识别图像中的多行?

java - 创建一个 View 类,它是两个 Android View 的组合

PHP url 识别

android - 使用 fragment 和 Activity 向上导航

android - 错误 : com. app.android.dagger.component.AppComponent (unscoped) may not reference scoped bindings:

java - Android 创建多选列表