android 获取网络图片

MainAcitivty.java

package com.xiaoming.getpicturefromnetwork;

import java.io.IOException;

import com.xiaoming.getpicturefromnetwork.service.ImageService;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

	Button mbtn_getPicture = null;
	ImageView miv_image = null;
	EditText  met_path = null;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		mbtn_getPicture = (Button) findViewById(R.id.getPicture);
		miv_image = (ImageView) findViewById(R.id.image);
		met_path  = (EditText) findViewById(R.id.path);
		
		mbtn_getPicture.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				new Thread(new Runnable () {
					Bitmap bitmap = null;
					public void run() {
						ImageService imageservice = new ImageService();
						String path = met_path.getText().toString();
						try{
							byte[] data = imageservice.getPicture(path);
							bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
							miv_image.post(new Runnable() {
								@Override
								public void run() {
									miv_image.setImageBitmap(bitmap);
								}
							});
			
						}catch (Exception e) {
							e.printStackTrace();
							Toast.makeText(MainActivity.this, "获取图片失败!", Toast.LENGTH_LONG).show();
						}
					}
					
				}).start();
				
			}
		});
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}




注意:


miv_image.post(new Runnable() {
	@Override
	public void run() {
	miv_image.setImageBitmap(bitmap);
	}
});

就是在这个部分很蛋疼啊, 如果直接写miv_image.setImageBitmap(bitmap); 就永远不能成功. 嗨, 多注意一点






ImageService.java

package com.xiaoming.getpicturefromnetwork.service;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import org.apache.http.client.methods.HttpUriRequest;

import com.xiaoming.getpicturefromnetwork.utils.StreamTool;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class ImageService  {
	
	public byte[] getPicture(String path) throws Exception{
		URL url = new URL(path);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setRequestMethod("GET");
		conn.setConnectTimeout(5000);
		if( 200 == conn.getResponseCode() ){
			InputStream is = conn.getInputStream();
			return StreamTool.readStream(is);
		}
		return null;
	}

		

}



StreamTool.java

package com.xiaoming.getpicturefromnetwork.utils;

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

import android.graphics.Bitmap;

public class StreamTool {

	/**
	 * 读取流中的数据
	 * @param in
	 * @return
	 * @throws Exception
	 */
	public static byte[] readStream(InputStream in) throws Exception {
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = 0;
		while( (len =in.read(buffer) ) != -1)
		{
			outStream.write(buffer,0,len);
		}
		in.close();
		return outStream.toByteArray();
	}

}





你可能感兴趣的:(android,实例,网络文件获取)