说明:仿黑马教程写的 

android一个小网络图片查看器(只能查看一张图片,因为图片的网址的死固定的),感谢黑马!

在学这部分内容是知道了httpwatch的作用及一些基本的用法;线程(主线程和子线程的一些关系及部分代码的掌握)


1.src源码:

package com.example.test;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

private ImageView iv = null;
private EditText et_path = null;
private Button bt_look = null;

public static final int CHANGE_UI = 1;
public static final int ERROR = 2;

//1.在主线程中创建消息处理器
@SuppressLint("HandlerLeak")
private Handler handler = new Handler() {
//3.接受处理消息
public void handleMessage(android.os.Message msg) {
if(msg.what == CHANGE_UI) {
Bitmap bitmap = (Bitmap) msg.obj;
iv.setImageBitmap(bitmap);
}
else if(msg.what == ERROR) {
Toast.makeText(MainActivity.this, "获取图片失败", Toast.LENGTH_SHORT).show();
}
};
};

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

iv = (ImageView) findViewById(R.id.iv);
et_path = (EditText) findViewById(R.id.et_path);
bt_look = (Button) findViewById(R.id.bt_look);

et_path.setText("http://img0.bdstatic.com/img/image/liuyifei_cover.png");

bt_look.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final String path = et_path.getText().toString().trim(); //要用final,因为内部类访问外部变量
if(TextUtils.isEmpty(path)) {
Toast.makeText(MainActivity.this, "图片的路径不能为空", Toast.LENGTH_SHORT).show();
}
else {
new Thread() {

public void run() {
//连接服务器,请求获取图片
try {
URL url = new URL(path);
//根据URL发送http的请求
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//设置请求的方式
conn.setRequestMethod("GET");
//设置连接超时的时间
conn.setConnectTimeout(5000);
//conn.setReadTimeout(timeoutMillis)
//设置请求参数 User-Agent浏览器来源

conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko");
//得到服务器返回的响应码
int code = conn.getResponseCode();
if(code == 200) {
InputStream is = conn.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(is);
//iv.setImageBitmap(bitmap);
//2.告诉主线程帮忙修改线程
Message msg = new Message();
msg.what = CHANGE_UI; //设置消息的类型
msg.obj = bitmap; //消息中存放的数据
handler.sendMessage(msg); //网消息队列中添加一天信息,
}
else {
//Toast.makeText(MainActivity.this, "获取图片失败", Toast.LENGTH_SHORT).show(); //Toast更新的也是UI界面,所以这里也要用子线程与主线程分开写
}
}
catch (Exception e) {
e.printStackTrace();
Message msg = new Message();
msg.what = ERROR;
handler.sendMessage(msg);
//Toast.makeText(MainActivity.this, "获取图片失败", Toast.LENGTH_SHORT).show(); //Toast更新的也是UI界面,所以这里也要用子线程与主线程分开写
}
};

}.start();
}
}
});
}

}


2.layout布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context="${packageName}.${activityClass}" >

<ImageView
android:id="@+id/iv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
/>

<!-- layout_weight
1. wrap_content 表示权重(值越大所占的比例就越大)
2. fill_parent 表示显示的优先级(值越大显示的越低)
-->

<EditText
android:id="@+id/et_path"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入图片的路径"
/>

<Button
android:id="@+id/bt_look"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="浏览"
/>

</LinearLayout>


3.显示结果:


4.特别注意:

在AndroidManifest.xml文件中要加上访问网络的权限,如下:

<uses-permission android:name="android.permission.INTERNET"/>


5.线程问题:

在Android 4.0以上的版本,不允许在主线程中更新UI界面,所以要把更新UI界面的操作放在子线程中,以下是说明主线程和子线程的操作关系






更多相关文章

  1. SpringBoot 2.0 中 HikariCP 数据库连接池原理解析
  2. android之ThreadLocal详解
  3. 自定义datePicker的实现
  4. Android(安卓)设置应用启动动画
  5. Android在子线程更新UI主线程的6种方法
  6. Java乔晓松-android中调用系统拍照功能并显示拍照的图片
  7. 【干货】快速理解Android(安卓)中的 Handler机制
  8. 解决android客户端上传图片到服务端时,图片损坏的问题
  9. android,actionbar,menu显示,图片,菜单禁用★★★

随机推荐

  1. Android(安卓)USB\HDMI等事件监听
  2. androidQ 访问SD卡权限问题
  3. Android Stagefright 技术相关网址
  4. Android屏幕点亮(常亮)及屏幕解锁和锁定
  5. Android Studio 日期选择器和时间选择器
  6. android 播放器开发准备工作
  7. Android:新手必备的常用代码片段整理(二)
  8. Android之WebView总结
  9. android Textview颜色渐变
  10. 内存管理 Memory Management for Android