Android डिवाइस पर उपलब्ध मेमोरी/स्टोरेज चेक करने का तरीका
यह उदाहरण StatFs और Formatter का उपयोग करके इंटरनल और एक्सटर्नल स्टोरेज पर उपलब्ध स्थान को मानव-पठनीय फॉर्मेट में दिखाता है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project चुनें।
चरण 2: लेआउट फ़ाइल (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:gravity="center"
android:padding="8dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
चरण 3: MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.text.format.Formatter;
import android.widget.TextView;
import java.io.File;
public class MainActivity extends AppCompatActivity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
// 1. एक्सटर्नल स्टोरेज (शेयर्ड/एसडी कार्ड)
File externalPath = Environment.getExternalStorageDirectory();
StatFs statExternal = new StatFs(externalPath.getPath());
long externalFreeBytes = statExternal.getAvailableBlocksLong() * statExternal.getBlockSizeLong();
// 2. इंटरनल स्टोरेज (ऐप डेटा डायरेक्टरी)
File internalPath = Environment.getDataDirectory();
StatFs statInternal = new StatFs(internalPath.getPath());
long internalFreeBytes = statInternal.getAvailableBlocksLong() * statInternal.getBlockSizeLong();
// मानव-पठनीय फॉर्मेट में बदलें
String externalFree = Formatter.formatFileSize(this, externalFreeBytes);
String internalFree = Formatter.formatFileSize(this, internalFreeBytes);
String result = "एक्सटर्नल फ्री: " + externalFree + "\n" +
"इंटरनल फ्री: " + internalFree;
textView.setText(result);
}
}
चरण 4: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="28" />
<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>
आधुनिक API (Android 10+/API 29+)
// StorageManager उपयोग करें - स्कोप्ड स्टोरेज के लिए सही StorageManager storageManager = getSystemService(StorageManager.class); StorageVolume volume = storageManager.getPrimaryStorageVolume(); long freeBytes = volume.getFreeBytes(); long totalBytes = volume.getTotalBytes(); String free = Formatter.formatFileSize(this, freeBytes);
नोट
Environment.getExternalStorageDirectory()API 29+ में डिप्रिकेटेड है।- ऐप-विशिष्ट फाइल्स के लिए
getFilesDir()याgetExternalFilesDir()उपयोग करें। Formatter.formatFileSize()बाइट्स को KB/MB/GB में ऑटो-कन्वर्ट करता है।