`
doll
  • 浏览: 82357 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

黑莓中从网络上取图片解析成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();
	}


已经通过模拟器测试。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics