Saturday, December 12, 2015

Get info about the space on a filesystem, using android.os.StatFs

android.os.StatFs is a wrapper for Unix statvfs(), to retrieve overall information about the space 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;

public class 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 = new StatFs(Environment.getRootDirectory().getAbsolutePath());

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {

            //The size, in bytes, of a block on the file system.
            long blockSize = statFs.getBlockSizeLong();
            textInfo.append("BlockSize: \t" + blockSize + "\n");

            //The total number of blocks on the file system.
            long blockCount = statFs.getBlockCountLong();
            textInfo.append("BlockCount: \t" + blockCount + "\n");

            //The total number of bytes supported by the file system.
            long totalBytes = statFs.getTotalBytes();
            textInfo.append("TotalBytes: \t" + totalBytes + "\n");

            //# of blocks that are free on the file system and available to applications.
            long availableBlocks = statFs.getAvailableBlocksLong();
            textInfo.append("AvailableBlocks: \t" + availableBlocks + "\n");

            //# of bytes that are free on the file system and available to applications.
            long availableBytes = statFs.getAvailableBytes();
            textInfo.append("AvailableBytes: \t" + availableBytes + "\n");

            //The total # of blocks that are free on the file system, including reserved blocks
            //(that are not available to normal applications).
            long freeBlocks = statFs.getFreeBlocksLong();
            textInfo.append("FreeBlocks: \t" + freeBlocks + "\n");

            //The total # of bytes that are free on the file system, including reserved blocks
            //(that are not 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="http://android-er.blogspot.com/"
        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>



No comments: