0

我正在尝试从服务器中提取 .jpg 图像,并将其显示为 .jpg和EncodedImage. ZoomScreen因为这个 .jpg 可能非常大,所以我想将 .jpg 保存到一个文件中并从那里读取它,所以我没有把整个东西放在内存中。

我面临的问题是 Connector.open("http.url.com/file.jpg") 要么抛出IOException带有消息“Bad Socket Id”的消息,要么ClassCastException在我尝试打开 a 时FileConnection抛出网址。这是我尝试过的一个例子:

    try {
        FileConnection fileIn = (FileConnection)Connector.open(fileURL);
        FileConnection fileOut = (FileConnection)Connector.open(fileDest);

        if(!fileOut.exists())
            fileOut.create();

        InputStream is = fileIn.openInputStream();
        OutputStream os = fileOut.openOutputStream();

        while(fileIn.canRead() && fileOut.canWrite()){
            os.write(is.read());
        }

        is.close();
        os.close();

        fileIn.close();
        fileOut.close();

        EncodedImage image = EncodedImage.getEncodedImageResource(fileDest);

        UiApplication.getUiApplication().pushScreen(new ZoomScreen(image));

    } catch (Exception e) {
        e.printStackTrace();
    }

我从 RIM 获得了大部分内容,但我遗漏了一些东西。我知道 url 是正确的,因为当我从同一服务器流式传输音频时,我使用相同的格式。当我尝试连接到服务器时,第一行抛出异常。

有人对此有经验吗?

4

2 回答 2

1

弄清楚了。

try {
    HttpConnection conn = (HttpConnection) Connector.open(fileURL);
    InputStream fileIn = conn.openInputStream();

    ByteArrayOutputStream os = new ByteArrayOutputStream();

    for(int i = fileIn.read(); i > -1; i = fileIn.read()){
        os.write(i);
    }

    byte[] data = os.toByteArray();

    EncodedImage image = EncodedImage.createEncodedImage(data, 0, data.length);

    UiApplication.getUiApplication().pushScreen(new ZoomScreen(image));

} catch (Exception e) {
    e.printStackTrace();
}

我希望其他人会发现这很有用。

于 2010-10-29T18:05:04.490 回答
1
public void run(){
    try{
        StreamConnection streamC = (StreamConnection) Connector.open(url+";deviceside=true");
        InputStream in = streamC.openInputStream();

        byte[] data = new byte[4096];
        data = IOUtilities.streamToBytes(in);
        in.close();
            EncodedImage eImage = EncodedImage.createEncodedImage(data, 0, data.length);
        }
    catch(Exception e)
        {
            e.printStackTrace();
        }
于 2011-03-23T14:19:18.903 回答