2011-02-09 6 views
1

主なアンドロイドデバイスで使用可能で使用されているディスクスペースを返すコード/簡単な機能。アンドロイドで使用されるディスクスペースは何ですか?

dfコマンドを作成して解析するのが最善の方法ですか?他にどのような方法を使用できますか?

多くのありがとうございました

+0

の可能重複[デバイスの内部利用可能なストレージを取得します]私は、システムコールを作ってみました(http://stackoverflow.com/questions/4562578/get-internal-available-storage-in-device) – CommonsWare

+0

dfコマンドを使用して出力を解析します。この目的のためのAPI関数は何ですか? – TDSii

答えて

9

私は素敵なクラスを修正することができました。

// PHONE STORAGE 
public static long phone_storage_free(){ 
    File path = Environment.getDataDirectory(); 
    StatFs stat = new StatFs(path.getPath()); 
    long free_memory = stat.getAvailableBlocks() * stat.getBlockSize(); //return value is in bytes 

    return free_memory; 
} 

public static long phone_storage_used(){ 
    File path = Environment.getDataDirectory(); 
    StatFs stat = new StatFs(path.getPath()); 
    long free_memory = (stat.getBlockCount() - stat.getAvailableBlocks()) * stat.getBlockSize(); //return value is in bytes 

    return free_memory; 
} 

public static long phone_storage_total(){ 
    File path = Environment.getDataDirectory(); 
    StatFs stat = new StatFs(path.getPath()); 
    long free_memory = stat.getBlockCount() * stat.getBlockSize(); //return value is in bytes 

    return free_memory; 
} 

// SD CARD 
public static long sd_card_free(){ 

    File path = Environment.getExternalStorageDirectory(); 
    StatFs stat = new StatFs(path.getPath()); 
    long free_memory = stat.getAvailableBlocks() * stat.getBlockSize(); //return value is in bytes 

    return free_memory; 
} 
public static long sd_card_used(){ 

    File path = Environment.getExternalStorageDirectory(); 
    StatFs stat = new StatFs(path.getPath()); 
    long free_memory = (stat.getBlockCount() - stat.getAvailableBlocks()) * stat.getBlockSize(); //return value is in bytes 

    return free_memory; 
} 
public static long sd_card_total(){ 

    File path = Environment.getExternalStorageDirectory(); 
    StatFs stat = new StatFs(path.getPath()); 
    long free_memory = stat.getBlockCount() * stat.getBlockSize(); //return value is in bytes 

    return free_memory; 
} 
+10

API> = 18 'getAvailableBlocks()'と 'getBlockSize()'は非推奨です。代わりに、 'getAvailableBlocksLong()'と 'getBlockSizeLong()'を使用してください。 –

+4

API> = 18の場合は、getAvailableBytes()を直接呼び出すことができます。ブロックとブロックサイズの手間は必要ありません。 –

関連する問題