android中使用的磁盘空间是什么?

TDS*_*Sii 1 android

一个代码/简单函数,它将返回主Android设备中的可用和已用磁盘空间.

正在制作df命令并以最佳方式解析它?还可以使用哪些其他方法?

提前谢谢了

TDS*_*Sii 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;
}
Run Code Online (Sandbox Code Playgroud)

  • API> = 18`不推荐使用getAvailableBlocks()和`getBlockSize()`.请改用`getAvailableBlocksLong()`和`getBlockSizeLong()`. (10认同)
  • 使用API​​> = 18,您可以直接调用getAvailableBytes(),无需使用块和块大小. (4认同)