查看网络图片

来源:互联网 发布:淘宝关闭订单 编辑:程序博客网 时间:2024/06/05 11:21

一   相关知识了解

1、什么是进程?
当一个程序开始运行时,它就是一个进程,进程包括运行中的程序和程序所使用到的内存和系统资源。
而一个进程又是由多个线程所组成的。

当一个程序开始运行时,它就是一个进程,进程包括运行中的程序和程序所使用到的内存和系统资源。
而一个进程又是由多个线程所组成的。

2、什么是多线程?
多线程是指程序中包含多个执行流,即在一个程序中可以同时运行多个不同的线程来执行不同的任务,也就是说允许单个程序创建多个并行执行的线程来完成各自的任务。
3、多线程的好处:

可以提高CPU的利用率。在多线程程序中,一个线程必须等待的时候,CPU可以运行其它的线程而不是等待,这样就大大提高了程序的效率。

二  实现过程   activity_main-xml

package org.bzu.imagebrowser;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.text.BreakIterator;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.Button;import android.widget.EditText;import android.widget.ImageView;import android.widget.Toast;public class MainActivity extends Activity {Button button;ImageView imageView;EditText ed;private static final int MSG_SUCCESS = 0;// 获取图片成功的标识private static final int MSG_FAILURE = 1;// 获取图片失败的标识@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);findId();}public void findId() {button = (Button) findViewById(R.id.btnView);imageView = (ImageView) findViewById(R.id.ivImage);ed = (EditText) findViewById(R.id.edt);}@Overridepublic 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;}private Handler handler = new Handler(){public void handleMessage(Message msg) {switch (msg.what) {case MSG_SUCCESS:Bitmap bit = (Bitmap) msg.obj;imageView.setImageBitmap(bit);break;default:break;}};};

<span style="color: rgb(51, 51, 51);">que</span><span style="color:#ff0000;">stion:对于一个UI界面中,当判断用户是否输入用户名或密码时,我们常用TextUtils.isEmpty()方法来判断;但有时也可以用这个equals()方法,都可以来判断EditText中是否为空,但有时很纠结,不知道这两种方法中哪个比较好?为什么?   answer:仔细读官方的API:  Returns true if the string is null or 0-length.  因为你从EditText返回的是一个变量。如果这个变量本身为null值,那么你掉它的equals方法是要报错的。但是如果你调用TextUtils.isEmpty() 把这个变量作为参数传进去。       只要这个参数为空或者为"",都会返回真。所以,用官方给的更加严谨。      而且,也十分方便。因为你单独去判断你还不是要写一个if语句判断。返回的还是一个boolean值</span>

<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"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <ImageView        android:id="@+id/ivImage"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:layout_weight="1" />    <EditText        android:id="@+id/edt"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:ems="10"        android:text="http://pic67.nipic.com/file/20150518/9885883_103055201000_2.jpg"         />    <Button        android:id="@+id/btnView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:onClick="viewImage"        android:text="浏览" /></LinearLayout>
三   采用匿名内部类的方法

public void viewImage(View view) {final String path = ed.getText().toString();//获取输入的图片路径if (TextUtils.isEmpty(path)) {Toast.makeText(MainActivity.this, " 路径不为空", Toast.LENGTH_SHORT).show();} else {new Thread() {public void run() {URL url;try {url = new URL(path);//发出请求HttpURLConnection connection = (HttpURLConnection) url.openConnection();connection.setRequestMethod("GET");//设置请求方式这里的方式必须为大写connection.setConnectTimeout(5000);//设置超时的时间int code = connection.getResponseCode();//获得状态码if (code == 200) {InputStream is = connection.getInputStream();Bitmap bitmap = BitmapFactory.decodeStream(is);//写入一个bitmap流Message m = new Message();m.what = MSG_SUCCESS;m.obj = bitmap;handler.sendMessage(m);} else {Toast.makeText(MainActivity.this, "打开失败",Toast.LENGTH_SHORT).show();}} catch (MalformedURLException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}};}.start();}}}

四  程序的效果图



0 0