Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了来自网址的Android图片视图大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在从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://scheR_776_11845@as.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>

清单文件

<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;
    }
}

大佬总结

以上是大佬教程为你收集整理的来自网址的Android图片视图全部内容,希望文章能够帮你解决来自网址的Android图片视图所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。