一、实验目的
(1)学会使用Handler消息处理机制
(2)理解主线程与子线程(在进行耗时的工作时,一定要创建子线程,再更新UI)
(3)理解HttpURLConnection类的使用
二、实验内容
输入网络图片的地址,点击查看按钮可以显示网络中的图片。
三、实验结果图
四、实验代码
MainActivity:
package com.test10.imageviewer;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class MainActivity extends Activity
{
private EditText edt_path;
private ImageView imageview;
private Handler handler = new Handler()
{
@Override
// 处理消息
public void handleMessage(Message msg)
{
// TODO Auto-generated method stub
Bitmap bitmap = (Bitmap) msg.obj;
imageview.setImageBitmap(bitmap);
if (bitmap == null)
{
Toast.makeText(MainActivity.this, "网络图片抓取失败", 0).show();
return;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt_path = (EditText) findViewById(R.id.address);
imageview = (ImageView) findViewById(R.id.image);
}
public void onclick(View view)
{
// TODO Auto-generated method stub
new Thread()// 子线程
{
public void run()
{
try
{
String path = edt_path.getText().toString().trim();
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setRequestMethod("GET");
// 设置请求方式
connection.setConnectTimeout(5000);// 设置超时时间
int code = connection.getResponseCode();
if (code == 200)
{
InputStream input = connection.getInputStream();// 获取数据
Bitmap bitmap = BitmapFactory.decodeStream(input);// 获取bitmap位图
Message msg = Message.obtain();
msg.obj = bitmap;
handler.sendMessage(msg);// 发消息
}
} catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
}
activity_main.xml:
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<EditText
android:id="@+id/address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/address"
android:hint="@string/hint" >
<requestFocus />
</EditText>
<Button
android:id="@+id/view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onclick"
android:text="@string/view" >
</Button>
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<uses-permission android:name="android.permission.INTERNET"/>
五、存在的问题
第一次实验的时候,使用的是setOnClickListener的方式写点击事件,发现并不成功,LogCat会一直报错:堆内存溢出
经过多次尝试,修改,最后在Button中加入了OnClick的方法,直接写出onclick(),终于可以正常运行程序。