2017-11-14 3 views
0

Ich versuche, den Speicherplatz auf Android zu bekommen. Hier ist die Lösung, die ich gefunden habe.StatFs gibt unterschiedliche Größe von der Einstellung

StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath()) 
stat.getTotalBytes() 

Es gibt 774 MB während die Einstellung App 1 GB zeigt. In AVD Manager ist die Größe auf der Festplatte 1 GB. Ist es möglich, den gleichen Wert wie die App zu erhalten?

Antwort

-1

unten sind Methoden zu überprüfen, ob es externe SD-Karte vorhanden ist, insgesamt Platz und Freiraum im internen Speicher und externe SD-Karte

public boolean isExternalSDCardAvailable() { 
    String secStore = System.getenv("SECONDARY_STORAGE"); 
    if (secStore != null) { 
     File f_secs = new File(secStore); 
     return f_secs.exists(); 
    } else return false; 
} 

public long getAvailableInternalMemorySize() { 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { 
     StatFs stat = new StatFs(Environment.getDataDirectory().getPath()); 
     long bytesAvailable = stat.getBlockSizeLong() * stat.getFreeBlocksLong(); 
     return bytesAvailable/(1024 * 1024); 
    } else { 
     StatFs stat = new StatFs(Environment.getDataDirectory().getPath()); 
     long bytesAvailable = (long) stat.getBlockSize() * (long) stat.getFreeBlocks(); 
     return bytesAvailable/(1024 * 1024); 
    } 
} 

public long getTotalInternalMemorySize() { 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { 
     StatFs stat = new StatFs(Environment.getDataDirectory().getPath()); 
     long bytesAvailable = stat.getBlockSizeLong() * stat.getBlockCountLong(); 
     return bytesAvailable/(1024 * 1024); 
    } else { 
     StatFs stat = new StatFs(Environment.getDataDirectory().getPath()); 
     long bytesAvailable = (long) stat.getBlockSize() * (long) stat.getAvailableBlocks(); 
     return bytesAvailable/(1024 * 1024); 
    } 
} 

public long getAvailableExternalMemorySize() { 
    String secStore = System.getenv("SECONDARY_STORAGE"); 
    File f_secs = new File(secStore); 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { 
     StatFs stat = new StatFs(f_secs.getPath()); 
     long bytesAvailable = stat.getBlockSizeLong() * stat.getAvailableBlocksLong(); 
     return bytesAvailable/(1024 * 1024); 
    } else { 
     StatFs stat = new StatFs(f_secs.getPath()); 
     long bytesAvailable = stat.getBlockSize() * stat.getAvailableBlocks(); 
     return bytesAvailable/(1024 * 1024); 
    } 

} 

public Long getTotalExternalMemorySize() { 
    String secStore = System.getenv("SECONDARY_STORAGE"); 
    File f_secs = new File(secStore); 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { 
     StatFs stat = new StatFs(f_secs.getPath()); 
     long bytesAvailable = stat.getBlockSizeLong() * stat.getBlockCountLong(); 
     long megAvailable = bytesAvailable/(1024 * 1024); 
     return megAvailable; 
    } else { 
     StatFs stat = new StatFs(f_secs.getPath()); 
     long bytesAvailable = stat.getBlockSize() * stat.getBlockCount(); 
     long megAvailable = bytesAvailable/(1024 * 1024); 
     return megAvailable; 
    } 

} 
+0

Bitte lesen Sie die Frage vor dem Beantworten (Copy-Paste). Ich kenne ** 'StatFs'. Ich frage ** WARUM ** es ist anders als die Wertanzeige in App einstellen. – Joshua

1

Versuchen,

StatFs stat1 = new StatFs("YOUR PATH HERE"); 
    long bytesAvailable1 = (long) stat1.getAvailableBlocks() * (long) stat1.getBlockSize(); 
    double spaceAvailableInSD1 = Double.parseDouble(new DecimalFormat("##.##").format(bytesAvailable1/1073741824d)); //return MB. 

Ich erhalte genau verfügbarer Speicherplatz in meinem Fall.

+0

'getTotalBytes()' ist 'getBlockCountLong() * getBlockCountLong()' wenn Sie den Quellcode lesen. Außerdem verwende ich den Code auf Android 8 Emulator. Sie sollten versuchen, Ihren Code zuerst darauf auszuführen. – Joshua

+0

@Joshua Meinten Sie getBlockSizeLong() * GetBlockCountLong() ist wie getTotalBytes() in der Quelle implementiert? – itisravi

+0

@itisavi https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/os/StatFs.java – Joshua

Verwandte Themen