黑莓:下载并显示.jpg图像

时间:2010-10-29 17:20:09

标签: blackberry download inputstream blackberry-simulator

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

我遇到的问题是Connector.open(“http.url.com/file.jpg”)正在抛出一个带有“Bad Socket Id”消息的IOException,或者它正在抛出一个当我尝试打开ClassCastException到网址时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获得了大部分内容,但我遗漏了一些东西。我知道网址是正确的,因为当我从同一台服务器传输音频时,我使用相同的格式。当我尝试连接到服务器时,第一行会抛出异常。

有没有人有这方面的经验?

2 个答案:

答案 0 :(得分: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();
}

我希望其他人会觉得这很有用。

答案 1 :(得分: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();
        }
相关问题