0
我想从黑莓中的URL构建一个BitmapImage。我怎样才能做到这一点?我如何构建黑莓中的BitmapImage
我想从黑莓中的URL构建一个BitmapImage。我怎样才能做到这一点?我如何构建黑莓中的BitmapImage
尝试使用以下代码:
Bitmap img=Bitmap.createBitmapFromBytes((getImageFromUrl(url)).getBytes()), 0,-1, 1);
其中getImageFromUrl(字符串URL)是从URL获取图像的方法。
public static String getImageFromUrl(String url) {
//Image img = null;
String imageData = null;
try
{
imageData = getDataFromUrl(url);
//img = Image.createImage(imageData.getBytes(), 0,imageData.length());
}
catch(Exception e1) {
e1.printStackTrace();
}
return imageData;
}
public static String getDataFromUrl(String url)
throws IOException {
StringBuffer b = new StringBuffer();
InputStream is = null;
HttpConnection c = null;
long len = 0 ;
int ch = 0;
ConnectionFactory connFact = new ConnectionFactory();
ConnectionDescriptor connDesc;
connDesc = connFact.getConnection(url);
if (connDesc != null)
{
//HttpConnection httpConn;
c = (HttpConnection)connDesc.getConnection();
}
// c = (HttpConnection)Connector.open(url);
is = c.openInputStream();
len = c.getLength();
if(len != -1) {
// Read exactly Content-Length bytes
for(int i =0 ; i < len ; i++)
if((ch = is.read()) != -1) {
b.append((char) ch);
}
} else {
//Read until the connection is closed.
while ((ch = is.read()) != -1) {
len = is.available() ;
b.append((char)ch);
}
}
is.close();
c.close();
return b.toString();
}
希望这将解决您的问题... :)
只是想标记一个珍闻到这一点。由于这是使用HTTP调用,所以请确保您在自己的Thread中运行此操作,因为它是阻止调用。 – jprofitt 2011-04-19 13:29:29