-2

可能重复:
在黑莓中显示来自 URL 的图像

嗨,我正在尝试直接从黑莓中的 url 加载图像。黑莓中没有直接调用的选项。有什么可以这样做的。请指导我。

4

1 回答 1

0

这样做没有捷径可走。您需要通过代码来完成。

public static InputStream getInputStream(String url)
{
     InputStream inputStream = null;
     HttpConnection httpConnection = null;
     try
     {                        
         httpConnection = (HttpConnection)Connector.open(url+connectiontype));
         httpConnection.setRequestMethod(HttpConnection.POST);
         final int r=httpConnection.getResponseCode();
         if(r==200)
             inputStream = httpConnection.openDataInputStream();
     }
     catch(final Exception e)
     {
         System.out.println("Error is "+e.getMessage());
     }
     return inputStream;
}

getInputStream()像这样使用:

InputStream is=getInputStream(s1);
int length=is.available();
//System.out.print("length ==========="+length);        
byte[] data=new byte[length];

data = IOUtilities.streamToBytes(is);

// code to save image in sd card
saveFile.create();
OutputStream outStream = saveFile.openOutputStream();
outStream.write(data);
outStream.close();
saveFile.close();
is.close();

此代码将从 url 保存图像。

于 2012-10-09T07:40:06.453 回答