0

我想检查我的设备 SD 卡上可用的可用大小。我知道FileConnectionAPI。如何查看 SD 卡的可用容量?

4

1 回答 1

2

想法是打开cfcard文件连接并调用availableSize()它。要获得正确的存储卡位置,请从System.getProperty(...). 在某些设备中,上述属性可能会失败,因此从存储卡名称系统属性构建新路径。

注意:在某些设备中,主机名必须是“localhost”,因此适当地更改 getFilehost() 中的返回字符串。

PS:由于这是一个代码片段,某些表单/命令引用可能会抛出空指针,请相应地解决它。

下面的代码为您提供存储卡“字节”中的可用大小

        String memoryCardPath = System.getProperty("fileconn.dir.memorycard");
        addToLogs(memoryCardPath);
        if (null == memoryCardPath) {
            displayAlert(null);
        }
        nativeHostname(memoryCardPath);
        addToLogs(nativeHostname);
        String path = buildPath(memoryCardPath.substring(getFilehost().length()));
        addToLogs(path);
        long availableSize = getAvailableSize(path);
        addToLogs(String.valueOf(availableSize)+" Bytes");
        if(availableSize == 0L) {
            String memoryCardName = System.getProperty("fileconn.dir.memorycard.name");
            addToLogs(memoryCardName);
            path = buildPath(memoryCardName + "/");
            addToLogs(path);
            availableSize = getAvailableSize(path);
            addToLogs(String.valueOf(availableSize)+" Bytes");
            if(availableSize == 0L) {
                displayAlert(null);
                return;
            }
        }

        displayAlert(String.valueOf(availableSize));

支持方式

public String buildPath(String foldername) {
    if(null == foldername) {
        foldername = "";
    }
    addToLogs("[BP]"+getFilehost());
    addToLogs("[BP]"+foldername);
    return new StringBuffer().append(getFilehost()).append(foldername).toString();
}

String nativeHostname = null;
public void nativeHostname(String url) {
    String host = url.substring("file://".length());
    addToLogs("[NH]"+host);
    int index = host.indexOf('/');
    addToLogs("[NH]"+String.valueOf(index));
    if(index > 0) {
        nativeHostname = host.substring(0, index);
    } else {
        nativeHostname = "/";
    }
    addToLogs("[NH]"+nativeHostname);
}

public boolean tryLocalhost = false;
public String getFilehost() {
    final StringBuffer buf = new StringBuffer().append("file://");
    if (null != nativeHostname && nativeHostname.length() > 0) {
         buf.append(nativeHostname);
    }
    addToLogs("[GFH] "+buf.toString());
    return buf.toString();
}

public long getAvailableSize(String path) {
    FileConnection fcon = null;
    try {
        fcon = (FileConnection) Connector.open(path, Connector.READ);
        if(null != fcon && fcon.exists()) {
            return fcon.availableSize();
        } else {
            return 0L;
        }
    } catch (IOException ex) {
        ex.printStackTrace();
        addToLogs("[GAS]"+"ERROR : "+ex.getMessage());
        return 0L;

    } finally {
        try {
            fcon.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

public void displayAlert(String text) {
    Alert alert = new Alert(
            null == text ? "Warning" : "Info", 
            null == text ? "Memory card not available" : text, 
            null, 
            null == text ? AlertType.WARNING : AlertType.INFO);
    alert.setTimeout(Alert.FOREVER);
    Display.getDisplay(this).setCurrent(alert, Display.getDisplay(this).getCurrent());
}

public void addToLogs(String log) {
    log = null == log ? "null" : log;
    getFormLogs().append(new StringItem("", log+"\n"));
}
于 2011-12-03T18:21:25.143 回答