Get Information Most The Infinite On A Filesystem, Using Android.Os.Statfs

Posted on

android.os.StatFs is a wrapper for Unix statvfs(), to retrieve overall data nearly the infinite on a filesystem.

Example:

MainActivity.java

package com.blogspot.android_er.androidstatfs;  import android.os.Bundle; import android.os.Environment; import android.os.StatFs; import android.support.v7.app.AppCompatActivity; import android.widget.TextView;  world cast MainActivity extends AppCompatActivity {      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          TextView textInfo = (TextView)findViewById(R.id.info);         textInfo.setText("Example of using StatFs:\n");         StatFs statFs = novel StatFs(Environment.getRootDirectory().getAbsolutePath());          if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {              //The size, inwards bytes, of a block on the file system.             long blockSize = statFs.getBlockSizeLong();             textInfo.append("BlockSize: \t" + blockSize + "\n");              //The full position out of blocks on the file system.             long blockCount = statFs.getBlockCountLong();             textInfo.append("BlockCount: \t" + blockCount + "\n");              //The full position out of bytes supported past times the file system.             long totalBytes = statFs.getTotalBytes();             textInfo.append("TotalBytes: \t" + totalBytes + "\n");              //# of blocks that are costless on the file organisation together with available to applications.             long availableBlocks = statFs.getAvailableBlocksLong();             textInfo.append("AvailableBlocks: \t" + availableBlocks + "\n");              //# of bytes that are costless on the file organisation together with available to applications.             long availableBytes = statFs.getAvailableBytes();             textInfo.append("AvailableBytes: \t" + availableBytes + "\n");              //The full # of blocks that are costless on the file system, including reserved blocks             //(that are non available to normal applications).             long freeBlocks = statFs.getFreeBlocksLong();             textInfo.append("FreeBlocks: \t" + freeBlocks + "\n");              //The full # of bytes that are costless on the file system, including reserved blocks             //(that are non available to normal applications).             long freeBytes = statFs.getFreeBytes();             textInfo.append("FreeBytes: \t" + freeBytes + "\n");         }else{             textInfo.append("SDK < JELLY_BEAN_MR2 (API Level 18)");         }      } }  

layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:padding="16dp"     android:orientation="vertical"     tools:context="com.blogspot.android_er.androidstatfs.MainActivity">      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_horizontal"         android:autoLink="web"         android:text="https://rootandroid.org//"         android:textStyle="bold"         android:textSize="26sp"/>      <TextView         android:id="@+id/info"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:textSize="20sp"/> </LinearLayout>