一.介绍
1.本Demo是利用tomcat搭建服务器,通过点击按钮获取服务器上的图片,并保存到本地缓存。
2.利用handler机制刷新UI线程。
二.代码块
public class MainActivity extends AppCompatActivity {
private Button button;
private File file;
private String path;
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case 0:
ImageView iv= (ImageView) findViewById(R.id.imageView);
iv.setImageBitmap((Bitmap) msg.obj);
Toast.makeText(MainActivity.this,"从网络获取图片",Toast.LENGTH_LONG).show();
break;
case 1:
Toast.makeText(MainActivity.this,"获取图片失败",Toast.LENGTH_LONG).show();
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button= (Button) findViewById(R.id.button);
}
public void click(View view) {
path = "http://192.168.3.2:8080/hehe.jpg";
file = new File(getCacheDir(), getFileName(path));
if (file.exists()) {
Bitmap bm= BitmapFactory.decodeFile(file.getAbsolutePath());
ImageView iv= (ImageView) findViewById(R.id.imageView);
iv.setImageBitmap(bm);
Toast.makeText(MainActivity.this,"缓存的图片",Toast.LENGTH_LONG).show();
}
else {
new Thread() {
@Override
public void run() {
//设置网络路径
//设置URL
try {
URL url = new URL(path);
//获取网络连接对象
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//初设连接设置,请求方式,读取超时,连接超时
conn.setRequestMethod("GET");
conn.setReadTimeout(5000);
conn.setConnectTimeout(5000);
//开启连接
conn.connect();
//连接成功
if (conn.getResponseCode() == 200) {
//获取输入流对象,此时请求的为一个图片对象
InputStream is = conn.getInputStream();
//保存文件到缓存
//创建文件
FileOutputStream fos = new FileOutputStream(file);
int lens = 0;
byte[] bytes = new byte[1024];
while ((lens = is.read(bytes)) != -1) {
fos.write(bytes, 0, lens);
}
fos.close();
Bitmap bm= BitmapFactory.decodeFile(file.getAbsolutePath());
//利用消息处理机制发送给主线程,请求更新UI
Message ms = handler.obtainMessage();
ms.obj=bm;
ms.what = 0;
handler.sendMessage(ms);
}
else {
Message ms = handler.obtainMessage();
ms.what = 1;
handler.sendMessage(ms);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
}
public String getFileName(String path){
int index=path.lastIndexOf("/");
return path.substring(index+1);
}
}
三.布局文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context="study.com.cacheinternet.MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Button" android:id="@+id/button" android:onClick="click" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true"/> <ImageView android:layout_width="200dp" android:layout_height="200dp" android:id="@+id/imageView" android:layout_centerVertical="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true"/> </RelativeLayout>四.在配置文件中添加访问网络的权限
<uses-permission android:name="android.permission.INTERNET"/>