Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the diskspace free and used in android?

Tags:

android

A code/simple function that will return the available and used diskspace in the main android device.

Is making a df command and parsing it the best way? What other methods can be used?

Many thanks in advance

like image 200
TDSii Avatar asked Dec 21 '22 18:12

TDSii


1 Answers

i managed to fix make a nice class.

// 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;
}
like image 113
TDSii Avatar answered Jan 10 '23 20:01

TDSii