views:

18

answers:

1

I am trying to pull a .jpg image off of a server, and display it as and EncodedImage in a ZoomScreen. Because this .jpg could be very large I want to save the .jpg to a file and read it from there so i don't have the whole thing sitting in memory.

The problem I'm facing is that Connector.open("http.url.com/file.jpg") is either throwing an IOException with the message "Bad Socket Id", or it is throwing a ClassCastException when i attempt to open a FileConnection to the URL. Here is an example of what i've tried:

    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();
    }

I'm getting most of this from RIM, but I'm missing something. I know the url is correct because i use the same format when i stream audio from the same server. The exception is being thrown on the first line, when i attempt to connect to the server.

Does anyone have experience with this?

+1  A: 

Figured it out.

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();
}

I hope other people will find this useful.

mtmurdock