当前位置: 代码迷 >> BlackBerry >> 黑莓中从网络下取图片解析成Bit地图代码片段
  详细解决方案

黑莓中从网络下取图片解析成Bit地图代码片段

热度:3009   发布时间:2013-02-26 00:00:00.0
黑莓中从网络上取图片解析成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();	}


已经通过模拟器测试。
  相关解决方案