Java me SD卡';s自由尺寸

Java me SD卡';s自由尺寸,java-me,file-connection,Java Me,File Connection,我想检查设备SD卡上可用的免费尺寸。我知道FileConnectionAPI。如何检查SD卡的可用大小?方法是打开cfcard文件连接并在其上调用availableSize()。要获取正确的存储卡位置,请从System.getProperty(…)读取它。在一些设备中,上述属性可能会失败,因此从存储卡名称系统属性构造新路径 注意:在某些设备中,主机名必须是“localhost”,因此请适当更改getFilehost()中的返回字符串 PS:由于这是一个代码段,某些表单/命令引用可能会抛出空指针,

我想检查设备SD卡上可用的免费尺寸。我知道
FileConnection
API。如何检查SD卡的可用大小?

方法是打开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"));
}