android一个小网络图片查看器

来源:互联网 发布:mac防蹭网软件 编辑:程序博客网 时间:2024/04/30 07:05
说明:仿黑马教程写的


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 0
原创粉丝点击