2010-05-31 3 views

Répondre

80

Pour obtenir l'espace « libre » de la carte SD externe disponible pour afficher un numéro qui est d'accord avec la Menu-> Paramètres-> carte SD et le numéro de stockage du téléphone, utilisez le code suivant:

StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath()); 
double sdAvailSize = (double)stat.getAvailableBlocks() 
        * (double)stat.getBlockSize(); 
//One binary gigabyte equals 1,073,741,824 bytes. 
double gigaAvailable = sdAvailSize/1073741824; 

documentation pertinente : http://developer.android.com/reference/android/os/StatFs.html

+0

+1 pour le code exemple –

+0

ce 1 est tellement utile. l'autre est une allumeuse. – murftown

+0

+1 Oui, c'est utile – Praveenkumar

13
/** 
* This class is designed to get available space in external storage of android. 
* It contains methods which provide you the available space in different units e.g 
* bytes, KB, MB, GB. OR you can get the number of available blocks on external storage. 
* 
*/ 
public class AvailableSpaceHandler { 

    //********* 
    //Variables 
    /** 
    * Number of bytes in one KB = 2<sup>10</sup> 
    */ 
    public final static long SIZE_KB = 1024L; 

    /** 
    * Number of bytes in one MB = 2<sup>20</sup> 
    */ 
    public final static long SIZE_MB = SIZE_KB * SIZE_KB; 

    /** 
    * Number of bytes in one GB = 2<sup>30</sup> 
    */ 
    public final static long SIZE_GB = SIZE_KB * SIZE_KB * SIZE_KB; 

    //******** 
    // Methods 

    /** 
    * @return Number of bytes available on external storage 
    */ 
    public static long getExternalAvailableSpaceInBytes() { 
     long availableSpace = -1L; 
     try { 
      StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath()); 
      availableSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return availableSpace; 
    } 


    /** 
    * @return Number of kilo bytes available on external storage 
    */ 
    public static long getExternalAvailableSpaceInKB(){ 
     return getExternalAvailableSpaceInBytes()/SIZE_KB; 
    } 
    /** 
    * @return Number of Mega bytes available on external storage 
    */ 
    public static long getExternalAvailableSpaceInMB(){ 
     return getExternalAvailableSpaceInBytes()/SIZE_MB; 
    } 

    /** 
    * @return gega bytes of bytes available on external storage 
    */ 
    public static long getExternalAvailableSpaceInGB(){ 
     return getExternalAvailableSpaceInBytes()/SIZE_GB; 
    } 

    /** 
    * @return Total number of available blocks on external storage 
    */ 
    public static long getExternalStorageAvailableBlocks() { 
     long availableBlocks = -1L; 
     try { 
      StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath()); 
      availableBlocks = stat.getAvailableBlocks(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return availableBlocks; 
    } 
} 
+0

Puis-je demander pourquoi le « .restat (...) »? Aussi je crois que vous avez fait une erreur en utilisant Environment.getDataDirectory() getPath() dans "getExternalAvailableSpaceInBytes()" au lieu de Environment.getExternalStorageDirectory() getPath() ... – Kasium

+0

fixe:.. Vous avez raison dans les deux questions, restat était pas nécessaire car l'objet StatFs est créé sur place et dispose des dernières informations. Et le chemin du répertoire de données a été ajouté par erreur. Merci. –

2

Ce code fonctionne pour moi:

StatFs stat = new StatFs(System.getenv("SECONDARY_STORAGE")); 

long bytesAvailable = (long)stat.getBlockSize() * (long)stat.getAvailableBlocks(); 

long kiloAvailable = bytesAvailable/1024; // Available space from SD in KB 
long megaAvailable = bytesAvailable/(1024*1024); // Available space from SD in MB 

return (int)kiloAvailable; 
0
public static float megabytesAvailable(String StrPath) { 
    StatFs stat = new StatFs(StrPath); 

    long bytesAvailable = (long)stat.getBlockSize() * (long)stat.getAvailableBlocks(); 
    return bytesAvailable/(1024.f * 1024.f); 
} 
Questions connexes