<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: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="com.example.dnshop.LoadImgActivity" > <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/etimgurl" android:hint="请输入图片地址" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="加载图片" android:id="@+id/btnLoad" android:onClick="btnLoad_Click" /> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/ivImag" /> </LinearLayout> </RelativeLayout>
package com.example.dnshop; import android.annotation.SuppressLint; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Handler; import android.os.Message; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class LoadImgActivity extends ActionBarActivity { //属性 private ImageView ivImag; private EditText etimgurl; private Thread mThread;//用于访问网络的 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_load_img); //初始化 ivImag = (ImageView) findViewById(R.id.ivImag); etimgurl = (EditText) findViewById(R.id.etimgurl); } //主线程建立Handler对象,用于线程之间的通信 private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { //处理线程返回的消息 //判断消息来自哪个线程 if(msg.what==1){ //获取图片,加载至界面 Bitmap bitmap = (Bitmap)msg.obj; ivImag.setImageBitmap(bitmap); }else { Toast.makeText(LoadImgActivity.this,"显示图片有误!",Toast.LENGTH_LONG).show(); } } }; //加载图片 public void btnLoad_Click(View view){ //获取图片的地址url final String imgPath = etimgurl.getText().toString().trim(); //判断图片路径是否为空 if(TextUtils.isEmpty(imgPath)){ //为空 Toast.makeText(this,"图片路径不能为空!",Toast.LENGTH_LONG).show(); }else{ //加载图片 mThread = new Thread(){ @Override public void run() { //加载图片 //loadImagesByHttpURLConnection(imgPath); loadImagesByHttpURLConnection(imgPath); } }; mThread.start();//启动线程 } } //访问网络,获取图片 private void loadImagesByHttpURLConnection(String imgPath){ try { URL url = new URL(imgPath);//新建url对象 HttpURLConnection connection = (HttpURLConnection)url.openConnection();//建立连接对象 connection.setReadTimeout(5000);//设置超市时间,单位毫秒。必须要设置 connection.setRequestMethod("GET");//提交方式 //获取放回的代码 int code = connection.getResponseCode();//200 - 访问成功 //判断 if(code==200){ //加载图片成功 //获取服务器返回的数据 InputStream is = connection.getInputStream();//输入流 Bitmap bitmap = BitmapFactory.decodeStream(is);//讲输入流转换为图片对象 //新建消息对象,发送至主线程 Message message = new Message(); message.what = 1; message.obj = bitmap; handler.sendMessage(message);//发送消息到主线程 }else { //加载图片失败 //新建消息对象,发送至主线程 Message message = new Message(); message.what = 0; handler.sendMessage(message);//发送消息到主线程 } } catch (Exception e) { e.printStackTrace(); //新建消息对象,发送至主线程 Message message = new Message(); message.what = 0; handler.sendMessage(message);//发送消息到主线程 } } //访问网络,获取图片 private void loadImagesByHttpClient(String imgPath){ try { HttpClient client = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(imgPath);//使用get访问 HttpResponse httpResponse = client.execute(httpGet);//访问网络图片 //获取返回的代码 int code = httpResponse.getStatusLine().getStatusCode();//200 - 访问成功 //判断 if(code==200){ //加载图片成功 //获取服务器返回的数据 HttpEntity entity = httpResponse.getEntity();//返回的数据 InputStream is = entity.getContent();//输入流 Bitmap bitmap = BitmapFactory.decodeStream(is);//讲输入流转换为图片对象 //新建消息对象,发送至主线程 Message message = new Message(); message.what = 1; message.obj = bitmap; handler.sendMessage(message);//发送消息到主线程 }else { //加载图片失败 //新建消息对象,发送至主线程 Message message = new Message(); message.what = 0; handler.sendMessage(message);//发送消息到主线程 } } catch (Exception e) { e.printStackTrace(); //新建消息对象,发送至主线程 Message message = new Message(); message.what = 0; handler.sendMessage(message);//发送消息到主线程 } } }