यह उदाहरण दर्शाता है कि Kotlin का उपयोग करके Android डिवाइस पर उपलब्ध इंटरनल और एक्सटर्नल स्टोरेज स्पेस कैसे चेक किया जाता है। StatFs और Formatter.formatFileSize() का उपयोग किया जाता है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio खोलें, File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें।
चरण 2: activity_main.xml में UI जोड़ें
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="Tutorials Point"
android:textAlignment="center"
android:textColor="@android:color/holo_green_dark"
android:textSize="32sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textAlignment="center"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
चरण 3: MainActivity.kt में स्टोरेज कैलकुलेशन
import android.os.Bundle
import android.os.Environment
import android.os.StatFs
import android.text.format.Formatter
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import java.io.File
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
val textView: TextView = findViewById(R.id.textView)
// एक्सटर्नल स्टोरेज (SD कार्ड/शेयर्ड स्टोरेज)
val stat = StatFs(Environment.getExternalStorageDirectory().path)
val bytesAvailable = stat.blockSizeLong * stat.availableBlocksLong
val externalFree = Formatter.formatFileSize(this, bytesAvailable)
val externalTotal = Formatter.formatFileSize(this, stat.blockSizeLong * stat.blockCountLong)
// इंटरनल स्टोरेज (ऐप डेटा)
val path: File = Environment.getDataDirectory()
val stat2 = StatFs(path.path)
val internalFree = Formatter.formatFileSize(this, stat2.availableBlocksLong * stat2.blockSizeLong)
val internalTotal = Formatter.formatFileSize(this, stat2.blockCountLong * stat2.blockSizeLong)
// ऐप-विशिष्ट कैश/फाइल्स डायरेक्टरी
val cacheDir = cacheDir
val filesDir = filesDir
val cacheFree = Formatter.formatFileSize(this, cacheDir.freeSpace)
val filesFree = Formatter.formatFileSize(this, filesDir.freeSpace)
textView.text = """
External Storage: $externalFree free of $externalTotal
Internal Storage: $internalFree free of $internalTotal
App Cache: $cacheFree free
App Files: $filesFree free
""".trimIndent()
}
}
चरण 4: आधुनिक API (Android 9+) - StorageManager
import android.os.storage.StorageManager
import android.os.storage.VolumeInfo
val storageManager = getSystemService(StorageManager::class.java)
val volumes = storageManager.volumeInfos
for (volume in volumes) {
val diskInfo = volume.disk
if (diskInfo != null) {
val freeBytes = storageManager.getFreeBytes(volume.fsUuid)
val totalBytes = storageManager.getTotalBytes(volume.fsUuid)
Log.d("Storage", "${volume.description}: ${Formatter.formatFileSize(this, freeBytes)} free")
}
}
चरण 5: AndroidManifest.xml कॉन्फ़िगर करें
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.q11">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
ऐप चलाएं
अपने Android डिवाइस को कंप्यूटर से कनेक्ट करें। Android Studio में किसी एक्टिविटी फ़ाइल को खोलें और टूलबार से Run आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें और ऐप चलने पर डिफ़ॉल्ट स्क्रीन देखें।
महत्वपूर्ण नोट्स:
getExternalStorageDirectory()Android 10+ (API 29) में डिप्रिकेटेड- स्कोप्ड स्टोरेज के साथ, ऐप्स केवल अपने ऐप-विशिष्ट डायरेक्टरी तक सीधी पहुंच रखते हैं
Environment.getExternalStorageDirectory()की जगहcontext.getExternalFilesDir(null)उपयोग करेंStatFsअभी भी काम करता है लेकिन मॉडर्नStorageManagerप्राथमिकता है