net.rim.device.api.io.file.FileIOException:黑莓中的资源文件系统

时间:2011-09-23 08:54:41

标签: exception blackberry

以下代码抛出 net.rim.device.api.io.file.FileIOException:文件系统资源不足此异常。

谁能告诉我它是怎么发生的?

public Bitmap loadIconFromSDcard(int index) {

    FileConnection fcon = null;
    Bitmap icon = null;
    InputStream is=null;
    try {

        fcon = (FileConnection) Connector.open(Shikshapatri.filepath + "i"
                + index + ".jpg", Connector.READ);
        if (fcon.exists()) {
            byte[] content = new byte[(int) fcon.fileSize()];
            int readOffset = 0;
            int readBytes = 0;
            int bytesToRead = content.length - readOffset;
            is = fcon.openInputStream();
            while (bytesToRead > 0) {
                readBytes = is.read(content, readOffset, bytesToRead);
                if (readBytes < 0) {
                    break;
                }
                readOffset += readBytes;
                bytesToRead -= readBytes;
            }

            EncodedImage image = EncodedImage.createEncodedImage(content,
                    0, content.length);
            image = resizeImage(image, 360, 450);
            icon = image.getBitmap();

        }

    } catch (Exception e) {
        System.out.println("Error:" + e.toString());
    } finally {
        // Close the connections
        try {
            if (fcon != null)
                fcon.close();
        } catch (Exception e) {
        }
        try {
            if (is != null)
                is.close();
                is = null;

        } catch (Exception e) {
        }
    }

    return icon;
}

提前致谢...

1 个答案:

答案 0 :(得分:1)

查看此BB dev论坛帖子 - http://supportforums.blackberry.com/t5/Java-Development/File-System-Out-of-Resources/m-p/105597#M11927

基本上,您应该保证在您不需要时立即关闭所有连接/流,因为OS中的连接数量有限(无论是文件连接还是http连接) 。如果您同时执行多个loadIconFromSDcard()调用(来自不同的线程),请考虑重新设计代码以按顺序调用它们。

<强>更新

为了避免在阅读content时出错,请使用以下内容:

byte[] content = IOUtilities.streamToBytes(is);

由于您不再需要文件连接和输入流,只需在阅读content之后立即关闭它们(在创建EncodedImage之前):

is.close();
is = null;    // let the finally block know there is no need to try closing it
fcon.close();
fcon = null;  // let the finally block know there is no need to try closing it

小点:

同样在finally块中,关闭它后明确值fcon = null;,我相信这可以帮助旧JVM(BB使用Java 1.3 - 相当旧的JVM)更快地决定对象准备垃圾收集了。

我也相信您在finally区块中关闭流的顺序可能很重要 - 我会先关闭is,然后再关fcon