Pages

Tuesday, August 16, 2016

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: ");
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: " + blockSize + " ");

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

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

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

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

//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: " + freeBlocks + " ");

//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: " + freeBytes + " ");
}else{
textInfo.append("SDK < JELLY_BEAN_MR2 (API Level 18)");
}

}
}


layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout

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>



Related Posts by Categories

0 comments:

Post a Comment