Android学习第十七天----从网络上获取图片
<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" 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="25dp" android:layout_marginTop="98dp" android:text="Button" /> <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button1" android:layout_alignParentTop="true" android:layout_marginLeft="27dp" android:layout_marginTop="16dp" android:ems="10" android:text="http://wenwen.soso.com/p/20101012/20101012083824-923841609.jpg"> </EditText> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/button1" android:layout_marginTop="88dp" android:src="@drawable/ic_launcher" /> </RelativeLayout>
在页面中设置一个按钮,然后点击,将图片显示在页面中
package com.example.service; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import android.graphics.Bitmap; import android.graphics.BitmapFactory; public class ImageService { public static byte[] getImage(String path) { // Bitmap bitmap = null; InputStream is = null; try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(5 * 1000); conn.setRequestMethod("GET"); if (conn.getResponseCode() == 200) { is = conn.getInputStream(); // bitmap = BitmapFactory.decodeStream(is); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return getImageByte(is); } public static byte[] getImageByte(InputStream is) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] b = new byte[1024]; int len = 0; try { while((len = is.read(b))!=-1) { baos.write(b, 0, len); } } catch (IOException e) { e.printStackTrace(); } return baos.toByteArray(); } }
mainActivity中
package com.example.httpimage; import com.example.service.ImageService; 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.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; public class MainActivity extends Activity { private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == 100) { image.setImageBitmap((Bitmap) msg.obj); } } }; private Button mButton; private ImageView image; private EditText edit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mButton = (Button) findViewById(R.id.button1); image = (ImageView) findViewById(R.id.imageView1); edit = (EditText) findViewById(R.id.editText1); mButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { new Thread(new Runnable() { @Override public void run() { Message msg = new Message(); msg.what = 100; BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 2; byte[] b = ImageService.getImage(edit.getText() .toString()); msg.obj = BitmapFactory.decodeByteArray(b, 0, b.length,options); handler.sendMessage(msg); } }).start(); } }); } }