显示网络中的图片

来源:互联网 发布:数据有效性检验 挖掘 编辑:程序博客网 时间:2024/05/16 17:12

丶Android中显示网络上的图片

最终效果图显示如下:

最终效果图

MainActivity.java主要代码展示:

package com.example.imageviews;import java.io.IOException;private Handler handler=new Handler(){          public void handleMessage(android.os.Message msg){              switch (msg.what) {              case 1:                  Bitmap bitmap=(Bitmap) msg.obj;                  ivName.setImageBitmap(bitmap);                  break;              default:                  break;              }          }      };      public void viewImage(View view){          final String path=etUrl.getText().toString();          if(TextUtils.isEmpty(path)){              Toast.makeText(this, "内容为空", Toast.LENGTH_LONG).show();          }else{             new Thread(){                public void run(){                   try {                         //链接服务器,获取图片                URL url=new URL(path);  HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();                          httpURLConnection.setRequestMethod("GET");                          httpURLConnection.setReadTimeout(5000);                         //设置链接超时时间 int op=httpURLConnection.getResponseCode();         if(op==200){            InputStream in=url.openStream();            Bitmap bitmap=BitmapFactory.decodeStream(in);                        Message msg=new Message();                        msg.what=1;                        msg.obj=bitmap;                        handler.sendMessage(msg);                          }                        else{                              Toast.makeText(MainActivity.this, "图片获取失败", Toast.LENGTH_LONG).show();                          }                      } catch (MalformedURLException e) {                          // TODO Auto-generated catch block                          e.printStackTrace();                      } catch (IOException e) {                          // TODO Auto-generated catch block                          e.printStackTrace();                      }                  };              }.start();          }      }  

activity_main.xml布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <ImageView        android:id="@+id/iglook"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_centerHorizontal="true"        android:layout_marginTop="72dp"        android:src="@drawable/ddd" />    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_centerHorizontal="true"        android:layout_marginBottom="31dp"        android:text="浏览" />    <EditText        android:id="@+id/etext"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_above="@+id/button1"        android:layout_centerHorizontal="true"        android:layout_marginBottom="35dp"        android:ems="10"        android:hint="请输入图片的路径" >        <requestFocus />    </EditText></RelativeLayout>
0 0
原创粉丝点击