首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > BlackBerry >

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

2012-09-28 
黑莓中从网络上取图片解析成Bitmap代码片段注:以下代码不是我自己写的,原文地址为:http://www.coderholic.

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


已经通过模拟器测试。

热点排行