从URL的Andr​​oid图像视图视图、图像、URL、Andr

2023-09-12 09:28:55 作者:你以为你是谁

我下载的URL的图像,但图像没有变化后,下载完成。 我在下面输入code,任何人都经历了同样的?

的Java 文件

 公共类MyImgActivity延伸活动{
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    ImageView的imgView =(ImageView的)findViewById(R.id.imageView1);
    可绘制绘制= LoadImageFromWebOperations("http://www.gophoto.it/view.php?i=https://m.xsw88.com/allimgs/daicuo/20230912/6258.png");

    imgView.setImageDrawable(绘制);
 }
私人绘制对象LoadImageFromWebOperations(字符串URL){
    尝试
      {
       InputStream的是=(InputStream的)新的网址(URL).getContent();
       绘制对象D = Drawable.createFromStream(是,SRC名);
       返回D组;
      }赶上(例外五){
       的System.out.println(EXC =+ E);
       返回null;
      }
}
}
 

XML 文件

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直>

< ImageView的
    机器人:ID =@ + ID / imageView1
    机器人:layout_height =match_parent
    机器人:layout_width =match_parent>< / ImageView的>
< / LinearLayout中>
 

清单文件

 <使用-权限的Andr​​oid:名称=android.permission.INTERNET对/>
 

解决方案

请点击下面code下载和显示图像为ImageView的。

 公共类形象延伸活动{
    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        点阵位图= DownloadImage("http://www.gophoto.it/view.php?i=https://m.xsw88.com/allimgs/daicuo/20230912/6258.png");
        ImageView的IMG =(ImageView的)findViewById(R.id.img);
        img.setImageBitmap(位);
    }

    私人的InputStream OpenHttpConnection(字符串urlString)抛出IOException异常{
        在的InputStream = NULL;
        INT响应= -1;

        网址URL =新的URL(urlString);
        URLConnection的康恩= url.openConnection();

        如果(!(康涅狄格州的instanceof的HttpURLConnection))
            抛出新的IOException异常(不是一个HTTP连接);

        尝试 {
            HttpURLConnection的httpConn =(HttpURLConnection类),康涅狄格州;
            httpConn.setAllowUserInteraction(假);
            httpConn.setInstanceFollowRedirects(真正的);
            httpConn.setRequestMethod(GET);
            httpConn.connect();
            响应= httpConn.getResponse code();
            如果(响应== HttpURLConnection.HTTP_OK){
                在= httpConn.getInputStream();
            }
        }赶上(例外前){
            抛出新的IOException异常(错误连接);
        }
        返回;
    }

    私人位图DownloadImage(字符串URL){
        点阵位图= NULL;
        在的InputStream = NULL;
        尝试 {
            在= OpenHttpConnection(URL);
            位= BitmapFactory.de codeStream(中);
            附寄();
        }赶上(IOException异常E1){
            // TODO自动生成的catch块
            e1.printStackTrace();
        }
        返回的位图;
    }
}
 

I am downloading an image from url, but the image is not changed after the download is complete. I am entering code below, anybody experienced the same?

Java file

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=https://m.xsw88.com/allimgs/daicuo/20230912/6258.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 file

<?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>

Manifest file

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

解决方案

Please Use below code for download and display image into 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=https://m.xsw88.com/allimgs/daicuo/20230912/6258.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;
    }
}