读书人

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

发布时间: 2012-09-28 00:03:35 作者: rapoo

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

热点推荐