加载Internet图片并显示在ImageView上。不可以在子线程中更改界面布局。
需要使用连接网络的权限: <uses-permission android:name="android.permission.INTERNET"/>
布局文件:
<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=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="45dp" android:layout_marginTop="16dp" android:text="Button" /> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button1" android:layout_below="@+id/button1" android:layout_marginLeft="24dp" android:layout_marginTop="80dp" android:src="@drawable/ic_launcher" /> </RelativeLayout>
Activity类:
package com.example.netdemo; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; 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.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity { private Button button; private ImageView imageView; private Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { //根据msg.what的值判断是哪个子线程 if(msg.what==11){ Bitmap mBitmap=(Bitmap) msg.obj; imageView.setImageBitmap(mBitmap); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button=(Button) findViewById(R.id.button1); imageView=(ImageView) findViewById(R.id.imageView1); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { //开启子线程 Thread thread=new Thread(new MyRunnable()); thread.start(); } }); } class MyRunnable implements Runnable{ @Override public void run() { String urlString="http://amorypepelu.github.io/shell/src/8eea0ecdd3c83d2.jpg"; InputStream is=null; try { //配置网络连接 URL url=new URL(urlString); HttpURLConnection connection=(HttpURLConnection) url.openConnection(); connection.setConnectTimeout(5*1000); connection.setRequestMethod("GET"); connection.setRequestProperty("", ""); is=connection.getInputStream(); //解析图片 Bitmap bitmap=BitmapFactory.decodeStream(is); //配置消息 Message msg=handler.obtainMessage(); msg.what=11; msg.obj=bitmap; //发送 handler.sendMessage(msg); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ if (is!=null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } } } }