3

I'd like to get the device storage capacity as displayed in Settings -> Storage - usually something like: 32 / 64 / 128 / 256 GB.

I used this post (and a few similar ones): It is possible to get total storage capacity?

I've got this code:

    StatFs stat = new StatFs(Environment.getDataDirectory().getAbsolutePath());
    long gb = 1024 * 1024 * 1024;
    long size = stat.getTotalBytes() / gb;

This returns 47GB on a 64GB Samsung S8 and 111GB on a 128GB Huawei Mate 10 - not what I was looking for.

How to get the 64GB / 128GB values? Thanks!!

Update

This is the same as this question: How to get exact capacity from storage. Unfortunately the accepted answer states that it's not possible to get the storage info - I was hoping to get a different answer .. maybe not using StatFs but some other way.

Daniel
  • 304
  • 2
  • 12
  • The total space is typically split into partitions (see for example https://forum.xda-developers.com/general/general/android-partitions-explained-t3657183). So not all is available in the file system. – Henry Oct 30 '18 at 05:35
  • Possible duplicate of [How to get exact capacity from storage](https://stackoverflow.com/questions/43004556/how-to-get-exact-capacity-from-storage) – Vikasdeep Singh Oct 30 '18 at 05:41
  • Thanks Henry - is it maybe possible not with StatFs but some other way? – Daniel Oct 30 '18 at 05:55
  • @Daniel did you check the answer by CommonsWare (he is **the Android Guru**) of duplicate question I mentioned above. It clearly states that it is not possible to get phone's storage capacity by any API (maybe possible for rooted devices). Check it: https://stackoverflow.com/a/43005006/631803 – Vikasdeep Singh Oct 31 '18 at 08:33
  • @VicJordan yes .. I saw, thanks .. I'll try for a few more days before giving in – Daniel Nov 01 '18 at 11:04
  • Did you get the answer.Kindly forward that to this link https://stackoverflow.com/questions/62797927/not-getting-correct-value-of-storage-volumeinternal – Gurarshdeep Singh Jul 12 '20 at 11:19

2 Answers2

4

You can try to use something like this (added in API level 26):

  1. use StorageManager#getStorageVolumes() https://developer.android.com/reference/android/os/storage/StorageManager#getStorageVolumes() to get all storage volumes UUID's

  2. via StorageStatsManager#getTotalBytes(java.util.UUID) https://developer.android.com/reference/android/app/usage/StorageStatsManager#getTotalBytes(java.util.UUID) you can find total bytes for each storage

  3. sum up sizes for all storages you need.

igor_rb
  • 1,821
  • 1
  • 19
  • 34
  • Thanks Igor - I tried this way - I get null from storageVolume.getUuid() .. also this would only work on very new devices (Android 8+) – Daniel Oct 31 '18 at 07:15
  • @Daniel When it's null, it means it's the primary one, so you need to use `StorageManager.UUID_DEFAULT` instead. Also, I think that the sum of what you get is only the storage that's ever can be available to the user (meaning without the system stuff). – android developer Jun 21 '19 at 07:41
-1

You can use below snippet for getting the total internal memory.

File path=Environment.getDataDirectory();
StatFs stat=new StatFs(path.getPath());
long blockSize=stat.getBlockSize();
long availableBlocks=stat.getBlockCount();
long totalInternalMemory=availableBlocks*blockSize;
Joy
  • 394
  • 2
  • 9