web-dev-qa-db-fra.com

Comment trouver la quantité de stockage libre (espace disque) restant sur Android?

J'essaie de déterminer l'espace disque disponible sur le téléphone Android qui exécute mon application. Y at-il un moyen de faire cela par programme? 

Merci,

28
Ashwin

Essayez StatFs.getAvailableBlocks . Vous aurez besoin de convertir le nombre de blocs en Ko avec getBlockSize.

13
Femi

Exemple: Obtenir une taille lisible par l’homme telle que 1 Go

Mémoire de chaîne = bytesToHuman (totalMemory ())

/*************************************************************************************************
Returns size in bytes.

If you need calculate external memory, change this: 
    StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
to this: 
    StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());        
**************************************************************************************************/
    public long totalMemory()
    {
        StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());   
        long   total  = (statFs.getBlockCount() * statFs.getBlockSize());
        return total;
    }

    public long freeMemory()
    {
        StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
        long   free   = (statFs.getAvailableBlocks() * statFs.getBlockSize());
        return free;
    }

    public long busyMemory()
    {
        StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());   
        long   total  = (statFs.getBlockCount() * statFs.getBlockSize());
        long   free   = (statFs.getAvailableBlocks() * statFs.getBlockSize());
        long   busy   = total - free;
        return busy;
    }

Conversion d'octets en format lisible par l'homme (comme 1 Mo, 1 Go)

    public static String floatForm (double d)
    {
       return new DecimalFormat("#.##").format(d);
    }


    public static String bytesToHuman (long size)
    {
        long Kb = 1  * 1024;
        long Mb = Kb * 1024;
        long Gb = Mb * 1024;
        long Tb = Gb * 1024;
        long Pb = Tb * 1024;
        long Eb = Pb * 1024;

        if (size <  Kb)                 return floatForm(        size     ) + " byte";
        if (size >= Kb && size < Mb)    return floatForm((double)size / Kb) + " Kb";
        if (size >= Mb && size < Gb)    return floatForm((double)size / Mb) + " Mb";
        if (size >= Gb && size < Tb)    return floatForm((double)size / Gb) + " Gb";
        if (size >= Tb && size < Pb)    return floatForm((double)size / Tb) + " Tb";
        if (size >= Pb && size < Eb)    return floatForm((double)size / Pb) + " Pb";
        if (size >= Eb)                 return floatForm((double)size / Eb) + " Eb";

        return "???";
    }
46
XXX

Il existe certaines subtilités concernant les chemins qu'aucune des réponses actuelles ne traite. Vous devez utiliser le bon chemin en fonction du type de statistiques qui vous intéresse. Basé sur une plongée approfondie dans DeviceStorageMonitorService.Java, qui génère les avertissements d'espace disque insuffisant dans la zone de notification et les diffusions persistantes d'ACTION_DEVICE_STORAGE_LOW que vous pouvez utiliser:

  1. Pour vérifier l'espace disque interne disponible, utilisez le répertoire de données obtenu via Environment.getDataDirectory (). Cela vous donnera l'espace libre sur la partition de données. La partition de données héberge tout le stockage interne pour toutes les applications du périphérique.

  2. Pour vérifier l'espace disque externe libre (SDCARD), utilisez le répertoire de stockage externe obtenu via Environment.getExternalStorageDirectory (). Cela vous donnera l’espace libre sur la carte SD.

  3. Pour vérifier la mémoire disponible sur la partition système contenant les fichiers du système d'exploitation, utilisez Environment.getRootDirectory (). Étant donné que votre application n'a pas accès à la partition système, cette statistique n'est probablement pas très utile. DeviceStorageMonitorService utilise à des fins d'information et entre dans un journal.

  4. Pour rechercher les fichiers temporaires/la mémoire cache, utilisez Environment.getDownloadCacheDirectory (). DeviceStorageMonitorService tente de nettoyer certains des fichiers temporaires lorsque la mémoire est insuffisante.

Quelques exemples de code permettant d’obtenir la mémoire libre interne (/ data), externe (/ sdcard) et OS (/ système):

// Get internal (data partition) free space
// This will match what's shown in System Settings > Storage for 
// Internal Space, when you subtract Total - Used
public long getFreeInternalMemory()
{
    return getFreeMemory(Environment.getDataDirectory());
}

// Get external (SDCARD) free space
public long getFreeExternalMemory()
{
    return getFreeMemory(Environment.getExternalStorageDirectory());
}

// Get Android OS (system partition) free space
public long getFreeSystemMemory()
{
    return getFreeMemory(Environment.getRootDirectory());
}

// Get free space for provided path
// Note that this will throw IllegalArgumentException for invalid paths
public long getFreeMemory(File path)
{
    StatFs stats = new StatFs(path.getAbsolutePath());
    return stats.getAvailableBlocksLong() * stats.getBlockSizeLong();
}
7
Theo

Sur la base de la réponse de @ XXX, j'ai créé un extrait de code Gist qui enveloppe les StatF pour une utilisation simple et facile .. Vous pouvez le trouver ici sous forme de GitHub Gist .

6
Tom Susel

Typecast vos valeurs entières bien avant la multiplication. La multiplication entre deux grands entiers pourrait déborder et donner un résultat négatif.

public long sd_card_free(){
    File path = Environment.getExternalStorageDirectory();
    StatFs stat = new StatFs(path.getPath());
    int availBlocks = stat.getAvailableBlocks();
    int blockSize = stat.getBlockSize();
    long free_memory = (long)availBlocks * (long)blockSize;

    return free_memory;
}
4
user802467

Avec un petit google, vous avez peut-être trouvé la StatFs- class qui est:

[...] un Wrapper pour Unix statfs ().

Des exemples sont fournis

3
Lukas Knuth
    File pathOS = Environment.getRootDirectory();//Os Storage
    StatFs statOS = new StatFs(pathOS.getPath());

    File pathInternal = Environment.getDataDirectory();// Internal Storage
  StatFs statInternal = new StatFs(pathInternal.getPath());

    File pathSdcard = Environment.getExternalStorageDirectory();//External(SD CARD) Storage
    StatFs statSdcard = new StatFs(pathSdcard.getPath());

    if((Android.os.Build.VERSION.SDK_INT < 18)) {
        // Get Android OS (system partition) free space API 18 & Below
        int totalBlocksOS = statOS.getBlockCount();
        int blockSizeOS = statOS.getBlockSize();
        int availBlocksOS = statOS.getAvailableBlocks();
        long total_OS_memory = (long) totalBlocksOS * (long) blockSizeOS;
        long free_OS_memory = (long) availBlocksOS * (long) blockSizeOS;
        long Used_OS_memory = total_OS_memory - free_OS_memory;
        TotalOsMemory       =   total_OS_memory ;
        FreeOsMemory        =   free_OS_memory;
        UsedOsMemory        =   Used_OS_memory;

        // Get internal (data partition) free space API 18 & Below
        int totalBlocksInternal = statInternal.getBlockCount();
        int blockSizeInternal = statOS.getBlockSize();
        int availBlocksInternal = statInternal.getAvailableBlocks();
        long total_Internal_memory = (long) totalBlocksInternal * (long) blockSizeInternal;
        long free_Internal_memory = (long) availBlocksInternal * (long) blockSizeInternal;
        long Used_Intenal_memory = total_Internal_memory - free_Internal_memory;
        TotalInternalMemory =   total_Internal_memory;
        FreeInternalMemory  =   free_Internal_memory ;
        UsedInternalMemory  =   Used_Intenal_memory ;

        // Get external (SDCARD) free space for API 18 & Below
        int totalBlocksSdcard = statSdcard.getBlockCount();
        int blockSizeSdcard = statOS.getBlockSize();
        int availBlocksSdcard = statSdcard.getAvailableBlocks();
        long total_Sdcard_memory = (long) totalBlocksSdcard * (long) blockSizeSdcard;
        long free_Sdcard_memory = (long) availBlocksSdcard * (long) blockSizeSdcard;
        long Used_Sdcard_memory = total_Sdcard_memory - free_Sdcard_memory;
        TotalSdcardMemory   =   total_Sdcard_memory;
        FreeSdcardMemory    =   free_Sdcard_memory;
        UsedSdcardMemory    =   Used_Sdcard_memory;
    }
    else {
        // Get Android OS (system partition) free space for API 18 & Above
        long   total_OS_memory          = (statOS.       getBlockCountLong()      * statOS.getBlockSizeLong());
        long   free_OS_memory           = (statOS.       getAvailableBlocksLong() * statOS.getBlockSizeLong());
        long Used_OS_memory = total_OS_memory - free_OS_memory;
        TotalOsMemory       =   total_OS_memory ;
        FreeOsMemory        =   free_OS_memory;
        UsedOsMemory        =   Used_OS_memory;

        // Get internal (data partition) free space for API 18 & Above
        long   total_Internal_memory    = (statInternal. getBlockCountLong()      * statInternal.getBlockSizeLong());
        long   free_Internal_memory     = (statInternal. getAvailableBlocksLong() * statInternal.getBlockSizeLong());
        long Used_Intenal_memory = total_Internal_memory - free_Internal_memory;
        TotalInternalMemory =   total_Internal_memory;
        FreeInternalMemory  =   free_Internal_memory ;
        UsedInternalMemory  =   Used_Intenal_memory ;

        // Get external (SDCARD) free space for API 18 & Above
        long   total_Sdcard_memory      = (statSdcard.   getBlockCountLong()      * statSdcard.getBlockSizeLong());
        long   free_Sdcard_memory       = (statSdcard.   getAvailableBlocksLong() * statSdcard.getBlockSizeLong());
        long Used_Sdcard_memory = tota*emphasized text*l_Sdcard_memory - free_Sdcard_memory;
        TotalSdcardMemory   =   total_Sdcard_memory;
        FreeSdcardMemory    =   free_Sdcard_memory;
        UsedSdcardMemory    =   Used_Sdcard_memory;
    }
}
2
gazi shaikh
/**
 * Returns the amount of free memory.
 * @return {@code long} - Free space.
 */
public long getFreeInternalMemory() {
    return getFreeMemory(Environment.getDataDirectory());
}

/**
 * Returns the free amount in SDCARD.
 * @return {@code long} - Free space.
 */
public long getFreeExternalMemory() {
    return getFreeMemory(Environment.getExternalStorageDirectory());
}

/**
 * Returns the free amount in OS.
 * @return {@code long} - Free space.
 */
public long getFreeSystemMemory() {
    return getFreeMemory(Environment.getRootDirectory());
}

/**
 * Returns the free amount in mounted path
 * @param path {@link File} - Mounted path.
 * @return {@code long} - Free space.
 */
public long getFreeMemory(File path) {
    if ((null != path) && (path.exists()) && (path.isDirectory())) {
        StatFs stats = new StatFs(path.getAbsolutePath());
        return stats.getAvailableBlocksLong() * stats.getBlockSizeLong();
    }
    return -1;
}

/**
 * Convert bytes to human format.
 * @param totalBytes {@code long} - Total of bytes.
 * @return {@link String} - Converted size.
 */
public String bytesToHuman(long totalBytes) {
    String[] simbols = new String[] {"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"};
    long scale = 1L;
    for (String simbol : simbols) {
        if (totalBytes < (scale * 1024L)) {
            return String.format("%s %s", new DecimalFormat("#.##").format((double)totalBytes / scale), simbol);
        }
        scale *= 1024L;
    }
    return "-1 B";
}

Depuis blocksize et getAvailableBlocks

sont obsolètes 

ce code peut être utilisé 

note basée sur la réponse ci-dessus par l'utilisateur802467

public long sd_card_free(){
    File path = Environment.getExternalStorageDirectory();
    StatFs stat = new StatFs(path.getPath());
    long availBlocks = stat.getAvailableBlocksLong();
    long blockSize = stat.getBlockSizeLong();
    long free_memory = availBlocks * blockSize;

    return free_memory;
}

nous pouvons utiliser getAvailableBlocksLong et getBlockSizeLong

1
1234567

Emplacements mémoire:

File[] roots = context.getExternalFilesDirs(null);
String path = roots[0].getAbsolutePath(); // PhoneMemory
String path = roots[1].getAbsolutePath(); // SCCard (if available)
String path = roots[2].getAbsolutePath(); // USB (if available)

usage

long totalMemory = StatUtils.totalMemory(path);
long freeMemory = StatUtils.freeMemory(path);

final String totalSpace = StatUtils.humanize(totalMemory, true);
final String usableSpace = StatUtils.humanize(freeMemory, true);

Vous pouvez utiliser ceci

public final class StatUtils {

    public static long totalMemory(String path) {
        StatFs statFs = new StatFs(path);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
            //noinspection deprecation
            return (statFs.getBlockCount() * statFs.getBlockSize());
        } else {
            return (statFs.getBlockCountLong() * statFs.getBlockSizeLong());
        }
    }

    public static long freeMemory(String path) {
        StatFs statFs = new StatFs(path);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
            //noinspection deprecation
            return (statFs.getAvailableBlocks() * statFs.getBlockSize());
        } else {
            return (statFs.getAvailableBlocksLong() * statFs.getBlockSizeLong());
        }
    }

    public static long usedMemory(String path) {
        long total = totalMemory(path);
        long free = freeMemory(path);
        return total - free;
    }

    public static String humanize(long bytes, boolean si) {
        int unit = si ? 1000 : 1024;
        if (bytes < unit) return bytes + " B";
        int exp = (int) (Math.log(bytes) / Math.log(unit));
        String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
        return String.format(Locale.ENGLISH, "%.1f %sB", bytes / Math.pow(unit, exp), pre);
    }
}
0
Samuel