如何从网络中获取png图片

1. J2ME

// 方法1
Connector conn = Connector.open(url, Connector.READ_WRITE, true);
InputStream is = ((HttpConnection) conn).openInputStream();
Image img = Image.createImage(is);

// 方法2
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] bytes = new byte[128];
int size = 0, totalSize = 0;
HttpConnection conn = (HttpConnection) Connector.open(url, Connector.READ_WRITE, true);
InputStream is = conn.openInputStream();
try {
	while ((size = is.read(bytes)) != -1) {
		baos.write(bytes, 0, size);
		totalSize += size;
	}
} catch (IOException e) {
	System.out.println("IOEx = " + e.toString());
}
Image img = Image.createImage(baos.toByteArray(), 0, totalSize);
bytes = null;
 

2. Android

// 方法1
public class MapAppl extends Activity {
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(new mapView(this));
	}
	private class mapView extends View {
		public mapView(Context context) {
			super(context);
		}
		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);
			Bitmap bm = null;
			try {
				InputStream is = getInputStream("http://www.tiexin.com/images/map_blocks/1/4/3/3/00000037_00000031.png");
				bm = BitmapFactory.decodeStream(is);
				if (is != null) {
					is.close();
					is = null;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			Paint p = new Paint();
			canvas.drawBitmap(bm, 40, 40, p);
		}
		
		private InputStream getInputStream(String strURL) throws IOException {
			URL url = new URL(strURL);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setDoInput(true);
			conn.setConnectTimeout(1000);
			conn.setRequestMethod("GET");
			conn.connect();
			for (int i = 0; i < 5; i++) { // 连接5次
				if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
					InputStream is = conn.getInputStream();
					if (conn != null) {
//						conn.disconnect(); // 在未对is进行处理前,不能conn.disconnect()
						conn = null;
					}
					return is;
				}
			}
			return null;
		}
	}
}

 

// 方法2
public class MapAppl2 extends Activity {
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(new mapView(this));
	}
	private class mapView extends View {
		public mapView(Context context) {
			super(context);
		}
		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);
			Bitmap bm = null;
			try {
				bm = getImage("http://www.tiexin.com/images/map_blocks/1/4/3/3/00000037_00000033.png");
			} catch (IOException e) {
				e.printStackTrace();
			}
			Paint p = new Paint();
			canvas.drawBitmap(bm, 40, 40, p);
		}
		public final Bitmap getImage(String strUrl) throws IOException {
			Bitmap bm;
			HttpClient httpclient = new DefaultHttpClient();
			HttpGet httpget = new HttpGet(strUrl);
			HttpResponse response = httpclient.execute(httpget);
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				InputStream input= entity.getContent();
				byte[] bitImage = new byte[1024*1024], data=new byte[256];
				int totalSize = 0, size = 0;
				while ((size = input.read(data)) != -1) {
					System.arraycopy(data, 0, bitImage, totalSize, size);
					totalSize += size;
				}
				bm = totalSize > 0 ? BitmapFactory.decodeByteArray(bitImage, 0, totalSize) : null;
			} else {
				bm = null;
			}
			// Do not feel like reading the response body
			// Call abort on the request object
			httpget.abort();
			return bm;
		}
	}
}
一般建议使用HttpClient进行连接,因为在redirect的时候URL不管用。 此外,Http连接后,一定要判断返回的statusCode(ResponseCode)。

你可能感兴趣的:(JavaME)