获取服务器Url图片资源,并显示在ImageView中 Android

在ImageView_test.xml文件中


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

<Button 

   android:id="@+id/lookView"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:text="查看图片"
   />
    <ImageView 
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        />
</LinearLayout>


在Activity中,

/**
 * 
 */
package com.example.webviewdemo;


import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


/**
 * <p>Title: Foundation SysLog         </p>
 * <p>Description: 
 * This is a change object in SysLog
 * </p>
 * <p>@author: subh                </p>
 * <p>Copyright: Copyright (c) 2012    </p>
 * <p>Company: FFCS Co., Ltd.          </p>
 * <p>Create Time: 2013年8月9日             </p>
 * <p>Update Time:                     </p>
 * <p>Updater:                         </p>
 * <p>Update Comments:                 </p>
 */
public class ImageViewTest extends Activity {



private Button btnLook;
private ImageView ivImageView;
private String urlPath=null;
private Bitmap bitmap;
private MyHandle mHandle;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO 自动生成的方法存根
super.onCreate(savedInstanceState);

setContentView(R.layout.imageview_test);

initComponent();
initData();
setLisenter();
}
public void initComponent(){

btnLook=(Button)findViewById(R.id.lookView);
ivImageView=(ImageView)findViewById(R.id.imageView);
mHandle=new MyHandle();
}
public void initData(){
urlPath=getIntent().getStringExtra("urlFile");
}
public void setLisenter(){
btnLook.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
DownloadImageRunner runnable=new DownloadImageRunner();
new Thread(runnable).start();   //调用线程
}
});

}
//创建线程
private class DownloadImageRunner implements Runnable {
       public void run() {
           Message msg = new Message();
           msg.what = 1;
           try {
               URL url;
               url = new URL(urlPath);
               HttpURLConnection conn;
               conn = (HttpURLConnection) url.openConnection();  //创建链接
               conn.setConnectTimeout(6000);   //超时设定
               conn.setDoInput(true);  
               conn.setUseCaches(false);  //不设置本地缓存
               InputStream is = conn.getInputStream();
               bitmap = BitmapFactory.decodeStream(is);    //将流转为Bitmap
               is.close();                 //关闭流
           } catch (IOException e) {
               msg.what = 0;
               e.printStackTrace();
           } catch (Exception e) {
               msg.what = 0;
               e.printStackTrace();
           }
           mHandle.sendMessage(msg);    //发送handle信息请求
       }        
   }
private class MyHandle extends Handler{
@Override
public void handleMessage(Message msg) {
// TODO 自动生成的方法存根
super.handleMessage(msg);
if (msg.what==1) {
ivImageView.setImageBitmap(bitmap);    //显示ImageView图片
} else {
Toast.makeText(ImageViewTest.this, "获取服务器图片失败", Toast.LENGTH_SHORT).show();
}
}
}
}







  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值