布局:
<?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="请输入地址"
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="获取并浏览" />
</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();
}
}
}