1、新建一个XML和一个Imgview,用来点击获取和显示图片
<span style="font-size:18px;"><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"
>
<ImageView
android:id="@+id/imgview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"</span>
2,、新建一个Activty继承Activty
<span style="font-size:18px;">package com.example.com.scxh.netimgw.wangqu;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Connection;
import android.app.Activity;
import android.database.CursorJoiner.Result;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final String httpUrls = "http://www.jimeise.net/wp-content/uploads/2014/05/IMGP6150.jpg";
private ImageView mImageView, ImageView2;
private Button mButton, mButton2;
Bitmap bitmap = null;
HttpURLConnection connect = null;
InputStream is = null;
private Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
Bitmap bitmap = (Bitmap) msg.obj;
mImageView.setImageBitmap(bitmap);
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = (ImageView) findViewById(R.id.imgview);
mButton = (Button) findViewById(R.id.butn);
mButton2 = (Button) findViewById(R.id.butns);
ImageView2 = (ImageView) findViewById(R.id.imgviews);
//用Handler进行网络取图片
// mButton.setOnClickListener(new OnClickListener() {
// public void onClick(View v) {
// new Thread() {
// public void run() {
// URL url;
// try {
// url = new URL(httpUrls);
// HttpURLConnection connect = (HttpURLConnection) url
// .openConnection();
// InputStream is = connect.getInputStream();
// Bitmap bitmap = BitmapFactory.decodeStream(is);
//
// Message msg = Message.obtain();
// msg.obj = bitmap;
// mHandler.sendMessage(msg);
// } catch (MalformedURLException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// } finally {
// if (is != null) {
// try {
// is.close();
// if (connect != null) {
// connect.disconnect();
// }
// } catch (IOException e) {
//
// e.printStackTrace();
// }
// }
// }
// }
//
// }.start();
//
// }
// });
</span>
这样就实现了网络取图片