Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to Get Internal Memory Storage Space in Android Programmatically?

  • Last Updated : 27 Sep, 2021

Every device has an internal memory where it can store files and applications. The internal memory of devices can vary anywhere between 4 GB to 512GB. As internal memory gets filled up with a stack of files and applications, the available space decreases. Developers, to save internal memory, design few applications so that they stay compressed when not running and expand once triggered. Now, if available space is less than the expanded version of a triggered application, the device might hang up, or the application might never open in the device.

So, to run such applications efficiently, it is necessary to keep track of internal memory. In this article, we will show you how you could get real-time information about internal memory.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Add a TextView in the layout to display the info.

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:layout_centerInParent="true"
        android:gravity="center"/>
  
</RelativeLayout>

Step 3: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

Kotlin




import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.os.StatFs
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import java.io.File
  
class MainActivity : AppCompatActivity() {
    @RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // TextView to show information
        val mTextView= findViewById<TextView>(R.id.text_view)
  
  
        // Fetching internal memory information
        val iPath: File = Environment.getDataDirectory()
        val iStat = StatFs(iPath.path)
        val iBlockSize = iStat.blockSizeLong
        val iAvailableBlocks = iStat.availableBlocksLong
        val iTotalBlocks = iStat.blockCountLong
        val iAvailableSpace = formatSize(iAvailableBlocks * iBlockSize)
        val iTotalSpace = formatSize(iTotalBlocks * iBlockSize)
  
        // Displaying the fetched info
        mTextView.text = "Internal Available: $iAvailableSpace\nInternal Total: $iTotalSpace"
  
    }
  
    // Function to convert byter to KB and MB
    private fun formatSize(size: Long): String? {
        var size = size
        var suffix: String? = null
        if (size >= 1024) {
            suffix = "KB"
            size /= 1024
            if (size >= 1024) {
                suffix = "MB"
                size /= 1024
            }
        }
        val resultBuffer = StringBuilder(java.lang.Long.toString(size))
        var commaOffset = resultBuffer.length - 3
        while (commaOffset > 0) {
            resultBuffer.insert(commaOffset, ',')
            commaOffset -= 3
        }
        if (suffix != null) resultBuffer.append(suffix)
        return resultBuffer.toString()
    }
}

Output:

You can see that when the application opens, the information regarding the internal memory is displayed.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!