SD Card's Free Size

678 views Asked by At

I want to check the free size available on My Device's SD Card. I know FileConnection API. How can I check the free size of the SD Card ?

1

There are 1 answers

0
Vimal On BEST ANSWER

The idea is to open the cfcard file connection and call availableSize() on it. to get the proper memory card location read it from the System.getProperty(...). In some devices the aforesaid property may fail hence constructing new path from the memory card name system property.

NOTE: In certain devices the hostname must be 'localhost', hence change the return string in getFilehost() appropriately.

PS: Since this is a code snippet certain form / command references may throw null pointer please resolve it accordingly.

Below code with get you the available size in 'BYTES' of the memory card

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

Supporting methods

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