0

デバイスのSDカードで利用可能な空きサイズを確認したい。私はFileConnectionAPIを知っています。SDカードの空きサイズを確認するにはどうすればよいですか?

4

1 に答える 1

2

アイデアは、cfcardファイル接続を開いてそれを呼び出すavailableSize()ことです。適切なメモリカードの場所を取得するには、からそれを読み取りますSystem.getProperty(...)。一部のデバイスでは、前述のプロパティが失敗する可能性があるため、メモリカード名システムプロパティから新しいパスを構築します。

注:特定のデバイスでは、ホスト名は「localhost」である必要があるため、getFilehost()の戻り文字列を適切に変更してください。

PS:これはコードスニペットであるため、特定のフォーム/コマンド参照がnullポインターをスローする可能性があるため、それに応じて解決してください。

以下のコードは、メモリカードの「BYTES」で利用可能なサイズを取得します

        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 に答える