黑莓中从网络上取图片解析成Bitmap代码片段

注:以下代码不是我自己写的,原文地址为: http://www.coderholic.com/blackberry-webbitmapfield/

直接上代码
private static Bitmap fetchImage( String imageURL, boolean useCaches )
	{
		
		EncodedImage encodedImage = null;
		if ( imageURL != null )
		{
			HttpConnection conn = null;
			InputStream is = null;			
	        try
			{
				// Open a new URL and get the InputStream to load data from it.
				conn = (HttpConnection)Connector.open(imageURL,Connector.READ,true);				
				is = conn.openInputStream();
				byte[] responseData = new byte[10000];
				int length = 0;
				StringBuffer rawResponse = new StringBuffer();
				while(-1 != (length = is.read(responseData))){
					rawResponse.append(new String(responseData,0,length));
				}				
				final String result = rawResponse.toString();
				byte[] dataArray = result.getBytes();
				encodedImage = EncodedImage.createEncodedImage(dataArray, 0, dataArray.length);					
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
			finally
			{	try{
				conn.close();
				is.close();}catch(Exception e){}
			}
		}
		
		return encodedImage.getBitmap();
	}


已经通过模拟器测试。

你可能感兴趣的:(BlackBerry,黑莓)