Method for obtaining storage space inside mobile phone and sdcard of Android

  • 2021-10-11 19:33:56
  • OfStack

Knowledge points:

The internal storage space obtains the total size and available size;

sdcard storage space obtains the total size and available size;

New term record {StatFs: class describing file system information}

Overview

In development, we will use storage space to store data more or less. There are two storage modes on mobile devices: the internal space of mobile phone and the external space of sdcard. Which one do you want to use for the storage space to be used? How to judge whether there is enough space? Is sdcard mounted? And so on, many problems come.

For the internal storage space of the device, it is 1 that will definitely exist. If internal storage space is used, what we need to do is to determine whether the internal storage space is large enough available space for us to use. Nowadays, mobile devices and mobile phone manufacturers have made internal storage very large, which is generally enough for users.

For sdcard, this is the user's external selective expansion of storage space. In fact, there will be sdcard. Especially at present, the internal storage space provided by Android mobile device manufacturers is getting larger and larger, and the starting ones are all 32G, and the larger ones can go to 128G, so users may not need to expand sdcard. However, the internal storage space of the device is small, and the need for external storage space may still occur. Moreover, we can know that the mainstream APP will choose to place some non-private data, such as pictures, songs and downloaded update packages, in sdcard to reduce the occupation of internal storage space.

Having said so much, it is necessary for us to explain the use of the above two kinds of storage space, how to obtain whether the storage space is available and whether the available space is large enough.

Internal storage space

As we know, the space of storage hardware is divided by blocks, and each block has a fixed size. Total size of storage device = block size * number of blocks. Through the StatFs class, we can get the total blocks, available blocks and the byte size of each block.

Look at the code specifically:


/**
   *  Get the total size of the internal space of the mobile phone 
   *
   * @return  Size, in bytes 
   */
  static public long getTotalInternalMemorySize() {
    // Get the internal storage root directory 
    File path = Environment.getDataDirectory();
    // Spatial description class of system 
    StatFs stat = new StatFs(path.getPath());
    // Bytes per block 
    long blockSize = stat.getBlockSize();
    // Total number of blocks 
    long totalBlocks = stat.getBlockCount();
    return totalBlocks * blockSize;
  }

  /**
   *  Get the available space inside the mobile phone 
   *
   * @return  Size, in bytes 
   */
  static public long getAvailableInternalMemorySize() {
    File path = Environment.getDataDirectory();
    StatFs stat = new StatFs(path.getPath());
    long blockSize = stat.getBlockSize();
    // Get the number of available blocks 
    long availableBlocks = stat.getAvailableBlocks();
    return availableBlocks * blockSize;
  }

With the above two methods, you can get the total size and available size of internal storage space.

sdcard External Storage Space


  /**
   *  Judge SD Is the card available 
   *
   * @return true :  Available <br>false :  Not available 
   */
  public static boolean isSDCardEnable() {
    return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
  }

/**
   *  Get the total external space of the mobile phone 
   *
   * @return  Total size in bytes 
   */
  static public long getTotalExternalMemorySize() {
    if (isSDCardEnable()) {
      // Get SDCard Root directory 
      File path = Environment.getExternalStorageDirectory();
      StatFs stat = new StatFs(path.getPath());
      long blockSize = stat.getBlockSize();
      long totalBlocks = stat.getBlockCount();
      return totalBlocks * blockSize;
    } else {
      return -1;
    }
  }

  /**
   *  Get SD Card remaining space 
   *
   * @return SD Card remaining space 
   */
  @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
  public static String getFreeSpace() {
    if (!isSDCardEnable()) return "sdcard unable!";
    StatFs stat = new StatFs(getSDCardPath());
    long blockSize, availableBlocks;
    availableBlocks = stat.getAvailableBlocksLong();
    blockSize = stat.getBlockSizeLong();
    long size = availableBlocks * blockSize / 1024L;
    return String.valueOf(size);
  }

System 1 describes the information of sdcard


/**
   *  Get SD Card information 
   *
   * @return SDCardInfo
   */
  @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
  public static String getSDCardInfo() {
    SDCardInfo sd = new SDCardInfo();
    if (!isSDCardEnable()) return "sdcard unable!";
    sd.isExist = true;
    StatFs sf = new StatFs(Environment.getExternalStorageDirectory().getPath());
    sd.totalBlocks = sf.getBlockCountLong();
    sd.blockByteSize = sf.getBlockSizeLong();
    sd.availableBlocks = sf.getAvailableBlocksLong();
    sd.availableBytes = sf.getAvailableBytes();
    sd.freeBlocks = sf.getFreeBlocksLong();
    sd.freeBytes = sf.getFreeBytes();
    sd.totalBytes = sf.getTotalBytes();
    return sd.toString();
  }

  public static class SDCardInfo {
    boolean isExist;
    long totalBlocks;
    long freeBlocks;
    long availableBlocks;
    long blockByteSize;
    long totalBytes;
    long freeBytes;
    long availableBytes;

    @Override
    public String toString() {
      return "isExist=" + isExist +
          "\ntotalBlocks=" + totalBlocks +
          "\nfreeBlocks=" + freeBlocks +
          "\navailableBlocks=" + availableBlocks +
          "\nblockByteSize=" + blockByteSize +
          "\ntotalBytes=" + totalBytes +
          "\nfreeBytes=" + freeBytes +
          "\navailableBytes=" + availableBytes;
    }
  }

Summarize

The above is an example of getting two different total and available storage space sizes respectively. I hope it will be useful to everyone.

If you have any questions, please contact me in time, thank you!


Related articles: