ตัวอย่างนี้สาธิตวิธีการตรวจสอบ พื้นที่จัดเก็บที่ว่างอยู่ (Available Storage Space) บนอุปกรณ์ Android โดยใช้ StatFs และ Environment
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
เปิด Android Studio ไปที่ File → New Project
ขั้นตอนที่ 2: Layout XML (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:textSize="24sp"
android:textStyle="bold"
android:textAlignment="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
ขั้นตอนที่ 3: MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.text.format.Formatter;
import android.util.Log;
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. ตรวจสอบ External Storage (SD Card / Internal Shared Storage)
StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
long blockSize = stat.getBlockSizeLong();
long availableBlocks = stat.getAvailableBlocksLong();
long availableBytes = availableBlocks * blockSize;
long availableMB = availableBytes / (1024 * 1024);
Log.d("Storage", "External Available MB: " + availableMB);
// 2. ตรวจสอบ Internal Storage (App Data Directory)
File path = Environment.getDataDirectory();
StatFs stat2 = new StatFs(path.getPath());
long blockSize2 = stat2.getBlockSizeLong();
long availableBlocks2 = stat2.getAvailableBlocksLong();
// ใช้ Formatter.formatFileSize เพื่อแปลงเป็นรูปแบบอ่านง่าย (KB, MB, GB)
String formattedSize = Formatter.formatFileSize(this, availableBlocks2 * blockSize2);
textView.setText(String.format("Available Memory: %s", formattedSize));
}
}
คำอธิบายเมธอดสำคัญ:
Environment.getExternalStorageDirectory()- ไดเรกทอรี Shared Storage (รูปภาพ, ดาวน์โหลด)Environment.getDataDirectory()- ไดเรกทอรีระบบ/แอป (/data)getBlockSizeLong()- ขนาดบล็อกหน่วยเป็นไบต์ (API 18+)getAvailableBlocksLong()- จำนวนบล็อกว่าง (API 18+)Formatter.formatFileSize()- แปลงไบต์เป็น String อ่านง่าย (เช่น "1.2 GB")
⚠️ ข้อควรระวัง (Android 10+ / API 29+):
- Scoped Storage - ไม่สามารถเข้าถึงไฟล์ของแอปอื่นได้โดยตรง
getExternalStorageDirectory()ยังทำงานได้แต่คืนค่า App-specific directory- สำหรับ Media Files ใช้
MediaStoreAPI แทน - สำหรับไฟล์ทั่วไป ใช้
Storage Access Framework(ACTION_OPEN_DOCUMENT)
วิธีที่แนะนำสำหรับ Android 10+ (StorageManager)
StorageManager storageManager = (StorageManager) getSystemService(STORAGE_SERVICE);
StorageVolume[] volumes = storageManager.getStorageVolumes();
for (StorageVolume volume : volumes) {
if (volume.isPrimary()) {
long totalBytes = volume.getTotalBytes();
long availableBytes = volume.getAvailableBytes();
String avail = Formatter.formatFileSize(this, availableBytes);
String total = Formatter.formatFileSize(this, totalBytes);
Log.d("Storage", "Primary: " + avail + " / " + total);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<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>
การทดสอบ
รันแอปพลิเคชัน จะแสดงพื้นที่ว่างในรูปแบบที่อ่านง่าย (เช่น "Available Memory: 2.5 GB")
