Method for obtaining SD card and mobile phone ROM capacity developed by Android

  • 2021-07-01 08:12:49
  • OfStack

In this paper, the method of obtaining the capacity of SD card and mobile phone ROM by Android is described as an example. Share it for your reference, as follows:

Here, through a simple small example, to obtain the capacity of SD card and the capacity of mobile phone ROM, the code is as follows:


package com.urovo.sdcardspace;
import java.io.File;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.app.Activity;
import android.text.format.Formatter;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView tv = (TextView) findViewById(R.id.tv);
    File path = Environment.getExternalStorageDirectory();// Get SD Path of card 
    StatFs stat = new StatFs(path.getPath());// Create StatFs Object used to get the state of the file system 
    long blockCount = stat.getBlockCount();
    long blockSize = stat.getBlockSize();
    long availableBlocks = stat.getAvailableBlocks();
    String totalSize = Formatter.formatFileSize(getApplicationContext(), blockCount*blockSize);// Format get SD Total card capacity 
    String availableSize = Formatter.formatFileSize(getApplicationContext(), blockCount*availableBlocks);// Obtain SD Available capacity of card 
    tv.setText("SD Total card capacity :"+totalSize+"\nSD Available capacity of card :"+availableSize+"\n"+getRomSpace());
  }
  private String getRomSpace() {
    File path = Environment.getDataDirectory();
    StatFs stat = new StatFs(path.getPath());
    long blockCount = stat.getBlockCount();
    long blockSize = stat.getBlockSize();
    long availableBlocks = stat.getAvailableBlocks();
    String totalSize = Formatter.formatFileSize(getApplicationContext(), blockCount*blockSize);
    String availableSize = Formatter.formatFileSize(getApplicationContext(), blockCount*availableBlocks);
    return " Mobile phone Rom Total capacity :"+totalSize+"\n Mobile phone Rom Available capacity :"+availableSize;
  }
}

Specific content, can be through the analysis of android settings part of the source code, to deepen understanding.

For more readers interested in Android related content, please check the topics on this site: "Summary of SD Card Operation Methods for Android Programming Development", "Introduction and Advanced Tutorial for Android Development", "Summary of Android Resource Operation Skills", "Summary of Android View View Skills" and "Summary of Android Control Usage"

I hope this article is helpful to everyone's Android programming.


Related articles: