In this video it shows the code to get the storage information (data) from your Android App. It shows example of fetching and displaying total storage available in the device and used storage. It uses a TextView to display the information.
I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: programmerworld1990@gmail.com
Code:
package com.programmerworld.storageinfoinandroidapp;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.view.View;
import android.widget.TextView;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
textView = findViewById(R.id.textView);
return insets;
});
}
public void buttonStorageInfo(View view){
StatFs statFs = new StatFs(Environment.getDataDirectory().getPath());
long blockSize = statFs.getBlockSizeLong(); // Size of each block
long totalBlocks = statFs.getBlockCountLong(); // Total number of blocks
long availableBlocks = statFs.getAvailableBlocksLong(); // Number of available blocks
long totalStorage = blockSize * totalBlocks; // Total internal storage in bytes
long availableStorage = blockSize * availableBlocks; // Available internal storage in bytes
long usedStorage = totalStorage - availableStorage; // Used internal storage in bytes
textView.setText("Total Storage: " + formatSize(totalStorage) + "\n" +
"Used Storage: " + formatSize(usedStorage));
}
// Helper function to format the size from bytes to readable format
public String formatSize(long size) {
String suffix = null;
float fSize = size;
if (fSize >= 1024) {
suffix = "KB";
fSize /= 1024;
if (fSize >= 1024) {
suffix = "MB";
fSize /= 1024;
}
if (fSize >= 1024) {
suffix = "GB";
fSize /= 1024;
}
}
StringBuilder resultBuffer = new StringBuilder(Float.toString(fSize));
int commaOffset = resultBuffer.indexOf(".");
if (commaOffset >= 0) {
int decimals = 3;
resultBuffer.setLength(commaOffset + decimals);
}
if (suffix != null) resultBuffer.append(suffix);
return resultBuffer.toString();
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="34sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="156dp"
android:layout_marginTop="80dp"
android:onClick="buttonStorageInfo"
android:text="Storage Info"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Screenshots:

