স্টোরেজ স্পেস চেক করা: StatFs ক্লাসের ব্যবহার
এই টিউটোরিয়ালটি দেখাবে কীভাবে StatFs ক্লাস ব্যবহার করে ইন্টারন্যাল এবং এক্সটার্নাল স্টোরেজের খালি জায়গা (বাইট/এমবি) বের করা যায়।
পদক্ষেপ ১: প্রজেক্ট তৈরি করুন
Android Studio-তে নতুন প্রজেক্ট তৈরি করুন।
পদক্ষেপ ২: লেআউট XML (activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
পদক্ষেপ ৩: MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.widget.TextView;
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);
// এক্সটার্নাল স্টোরেজ (পাবলিক ফাইলস - Pictures, Downloads ইত্যাদি)
File externalStorage = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(externalStorage.getPath());
long blockSize = stat.getBlockSizeLong();
long availableBlocks = stat.getAvailableBlocksLong();
long freeBytes = blockSize * availableBlocks;
long freeMB = freeBytes / (1024 * 1024);
// ইন্টারন্যাল স্টোরেজ (অ্যাপ-স্পেসিফিক ডেটা)
File internalStorage = Environment.getDataDirectory();
StatFs statInternal = new StatFs(internalStorage.getPath());
long internalFreeBytes = statInternal.getBlockSizeLong() * statInternal.getAvailableBlocksLong();
long internalFreeMB = internalFreeBytes / (1024 * 1024);
String result = String.format(
"External Storage Free: %d MB\nInternal Storage Free: %d MB",
freeMB, internalFreeMB
);
textView.setText(result);
}
}
গুরুত্বপূর্ণ পারমিশন (Android 10+/API 29+)
Android 10 (Q) থেকে Scoped Storage eingeführt হয়েছে। এক্সটার্নাল স্টোরেজে সরাসরি অ্যাক্সেস সীমিত।
- Android 9 (API 28) এবং নিচে: `READ_EXTERNAL_STORAGE`/`WRITE_EXTERNAL_STORAGE` পারমিশন দরকার।
- Android 10 (API 29): `requestLegacyExternalStorage="true"` ম্যানিফেস্টে অ্যাপ্লিকেশন ট্যাগে যোগ করুন (টেম্পোরারি)।
- Android 11 (API 30+): শুধু অ্যাপ-স্পেসিফিক ডিরেক্টরি (`getExternalFilesDir()`) এবং MediaStore এক্সেসibles। `MANAGE_EXTERNAL_STORAGE` পারমিশন (বিশেষ অনুমতি) দরকার সব ফাইল এক্সেসের জন্য।
আধুনিক পদ্ধতি: StorageManager (API 24+)
StorageManager storageManager = (StorageManager) getSystemService(STORAGE_SERVICE); Listvolumes = storageManager.getStorageVolumes(); for (StorageVolume volume : volumes) { if (volume.isPrimary()) { // প্রাইমারি স্টোরেজ long freeBytes = 0; try { freeBytes = volume.createOpenDocumentTree().getFreeSpace(); } catch (IOException e) { e.printStackTrace(); } } }
Formatter.formatFileSize() ব্যবহার (হিউম্যান রিডেবল)
String readableSize = Formatter.formatFileSize(this, freeBytes); // আউটপুট: "1.2 GB", "500 MB" ইত্যাদি
বেস্ট প্র্যাকটিস
- ফাইল লিখার আগে স্পেস চেক করুন
- বড় ডাউনলোডের আগে ইউজারকে সতর্ক করুন
- ক্যাশ ক্লিয়ার করার অপশন দিন
- WorkManager-এ periodic cleanup শিডিউল করুন