网络获取图片实例

/**
 * ImageViewAtivity.java
 * */
package cn.captain.imageview;

import cn.captain.service.ImageService;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class ImageViewActivity extends Activity {
    /** Called when the activity is first created. */
	private static final String TAG = "ImageViewActivity";
	ImageView imageView;
	EditText pathText;
    @Override
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button =(Button) this.findViewById(R.id.button);
        pathText = (EditText)this.findViewById(R.id.urlpath);
        imageView = (ImageView)this.findViewById(R.id.imageview);
        button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String path  = pathText.getText().toString();
			   try {
				byte[] data = ImageService.getImage(path);
				 Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);//生成位图
				 imageView.setImageBitmap(bitmap);//显示图片
			} catch (Exception e) {
				// TODO Auto-generated catch block
				Toast.makeText(ImageViewActivity.this, R.string.error,1).show();
				Log.e(TAG, e.toString());
			}
			}
		});
         
    }
}

/**
 * ImageService.java
 * */
package cn.captain.service;


import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import cn.captain.utils.StreamTool;



public class ImageService {
	
	public static byte[] getImage(String path) throws Exception
	{
		URL url = new URL("http://m4.auto.itc.cn/car/120/95/90/Img1579095_120.jpg"); 
		HttpURLConnection conn = (HttpURLConnection)url.openConnection();
		conn.setRequestMethod("GET");
		conn.setConnectTimeout(5 * 1000);
		InputStream inStream =  conn.getInputStream();//通过输入流获取图片数据	
		return StreamTool.readInputStream(inStream);//得到图片的二进制数据
		}
}

/**
 * ImageTool.java
 * */
package cn.captain.utils;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

public class StreamTool {
	public static byte[] readInputStream(InputStream instream) throws Exception
    {
	ByteArrayOutputStream outStream = new ByteArrayOutputStream();
	byte[]  buffer = new byte[1204];
	int len = 0;
	while ((len = instream.read(buffer)) != -1)
	{
		outStream.write(buffer,0,len);
	}
	instream.close();
	return outStream.toByteArray();     	
	}
}

<!--访问网络权限  -->
 <uses-permission android:name="android.permission.INTERNET"/> 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/urlpath" />
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="http://m4.auto.itc.cn/car/120/95/90/Img1579095_120.jpg"
        android:id="@+id/urlpath"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button" 
        android:id="@+id/button"/>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageview"/>

</LinearLayout>

网络获取图片实例

你可能感兴趣的:(网络获取图片)