Android网络应用(图片查看器)

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	android: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">
<ImageView
	android:id="@+id/iv"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:layout_weight="1000" />
<EditText
	android: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()
	{
		@Override
		public 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();
			}

		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	@Override
	public 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、Reffer
						conn.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();
		}
	}
}


转载于:https://my.oschina.net/u/1246663/blog/196429

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值