布局:

<?xml version="1.0" encoding="utf-8"?><LinearLayoutandroid:id="@+id/widget32"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"xmlns:android="http://schemas.android.com/apk/res/android"><ImageViewandroid:id="@+id/iv"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_weight="1000" /><EditTextandroid:id="@+id/et"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="&#35831;&#36755;&#20837;&#22320;&#22336;"android:textSize="18sp"android:text="http://i7.hexunimg.cn/2013-03-29/152620496.jpg"android:gravity="center" /><Button    android:onClick="click"android:id="@+id/btn"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="&#33719;&#21462;&#24182;&#27983;&#35272;" /></LinearLayout>

核心Activity:

package com.pas.imgviewer;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.text.TextUtils;import android.view.Menu;import android.view.View;import android.widget.EditText;import android.widget.ImageView;import android.widget.Toast;public class MainActivity extends Activity{protected static final int CHANGE_UI = 1;protected static final int TOAST = 2;private Handler handler = new Handler(){@Overridepublic void handleMessage(android.os.Message msg){if (msg.what == CHANGE_UI){ImageView iv = (ImageView) findViewById(R.id.iv);iv.setImageBitmap((Bitmap) msg.obj);}if (msg.what == TOAST){Toast.makeText(MainActivity.this, msg.obj.toString(), Toast.LENGTH_SHORT).show();}};};@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}@Overridepublic boolean onCreateOptionsMenu(Menu menu){// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}public void click(View view){final String imgurl = ((EditText) findViewById(R.id.et)).getText().toString().trim();if (TextUtils.isEmpty(imgurl)){Toast.makeText(this, "路径不可为空", Toast.LENGTH_SHORT).show();} else{///*// * android 4.0开始网络操作不可以在主线程中进行 会导致ANR// * 需要在子线程中进行 // * 导致ANR原因,主线程做过多耗时的操作// * // */new Thread(){public void run(){try{// 链接服务器URL url = new URL(imgurl);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setRequestMethod("GET");conn.setConnectTimeout(5000);conn.setReadTimeout(10000);// 请求参数// 1、浏览器版本conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1");// 2、Refferconn.setRequestProperty("Referer", "http://image.baidu.com/i?ct=503316480&z=0&tn=baiduimagedetail&ipn=d&word=K29&step_word=&ie=utf-8&in=11493&cl=2&" + "lm=-1&st=-1&pn=4&rn=1&di=76845442520&ln=1951&fr=&&fmq=1390817152763_R&ic=0&s=&se=1&sme=0&tab=&width=0&height=0&face=0&is=&istype=2&ist=&jit=" + "&objurl=http%3A%2F%2Fi7.hexunimg.cn%2F2013-03-29%2F152620496.jpg#pn4&-1&di76845442520&objURLhttp%3A%2F%2Fi7.hexunimg.cn%2F2013-03-29%2F152620496.jpg&" + "fromURLippr_z2C%24qAzdH3FAzdH3Fpjvi_z%26e3Bijx7g_z%26e3Bv54AzdH3Fda8n-an-dlAzdH3F8cdmda9l9_z%26e3Bip4s&W500&H375&T5700&S32&TPjpg");int resCode = conn.getResponseCode();if (resCode == -1){Toast.makeText(MainActivity.this, "获取失败,请检查地址,Code=" + resCode, Toast.LENGTH_SHORT).show();} else if (resCode == 200){InputStream ins = conn.getInputStream();Bitmap img = BitmapFactory.decodeStream(ins);// ImageView iv = (ImageView) findViewById(R.id.iv);// iv.setImageBitmap(img);///*// * 子线程中不可以修改UI // * 刷新UI只能在UI线程当中// * 此处利用android消息处理机制解决// */Message msg = new Message();msg.what = CHANGE_UI;msg.obj = img;handler.sendMessage(msg);} else{Message msg = new Message();msg.what = TOAST;msg.obj = "获取失败,请检查地址,Code=" + resCode;handler.sendMessage(msg);}} catch (Exception e){e.printStackTrace();Message msg = new Message();msg.what = TOAST;msg.obj = "获取失败";handler.sendMessage(msg);}}}.start();}}}


更多相关文章

  1. android常用的网络请求方式
  2. Android:网络:多线程断点下载
  3. Android实现订单倒计时功能
  4. Android(安卓)获取手机应用信息
  5. 一个android 异步多线程类介绍
  6. unity android获取电量和wifi信号强度
  7. Android(安卓)获取验证码倒计时
  8. Android中的数据存储方式
  9. Android中获取全局Context

随机推荐

  1. android上传图片到服务器(使用base64字节
  2. Android(安卓)Activity launchMode研究
  3. 软键盘弹出的时候界面被挤上去的问题
  4. Android Studio设置
  5. android有用代码片段(1)
  6. Android Bluetooth蓝牙开发:Bluetooth蓝牙
  7. android中的自动链接
  8. android_跑马灯
  9. [置顶] Android 动画animation 深入分析
  10. java/Android(安卓)error系列01: Value o