บทนำ
ตัวอย่างนี้สาธิตวิธีดึงเส้นทางของ Root Directory ของระบบ Android โดยใช้ Environment.getRootDirectory() ซึ่งมักจะเป็น /system
ขั้นตอนที่ 1: สร้างโปรเจกต์
Android Studio → File → New Project → Empty Activity
ขั้นตอนที่ 2: Layout (activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".MainActivity">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30sp" />
</LinearLayout>
ขั้นตอนที่ 3: โค้ดหลัก (MainActivity.java)
package com.example.myapplication;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView;
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.text);
// ดึง Absolute Path ของ Root Directory
textView.setText(Environment.getRootDirectory().getAbsolutePath().toString());
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onResume() {
super.onResume();
}
}
ผลลัพธ์

Android Partition Structure (ภาพรวม)
| Partition | Mount Point | คำอธิบาย | เมธอด Environment |
|---|---|---|---|
| Boot | /boot | Kernel, Ramdisk | - |
| System | /system | OS Framework, Pre-installed Apps (Read-only) | getRootDirectory() |
| Vendor | /vendor | Vendor-specific HAL, Libraries | - |
| Data | /data | User Data, Apps, Settings (Read-Write) | getDataDirectory() |
| Cache | /cache | Temporary System Cache | getDownloadCacheDirectory() (Deprecated) |
| Internal Storage | /data/data/pkg | App Private Data | context.getFilesDir() |
| External Storage | /sdcard 或 /storage/emulated/0 | Shared User Storage | getExternalStorageDirectory() (Deprecated) |
ข้อควรระวัง
- Read-only: /system เป็น Read-only ปกติ (Dynamic Partitions / A/B Partitions)
- ไม่ควรเขียนไฟล์: แอปทั่วไปไม่มีสิทธิ์เขียนใน /system
- Scoped Storage: Android 10+ จำกัดการเข้าถึง Shared Storage ให้ใช้ App-Specific Directory
- Root Access: การเข้าถึง /system ต้องการ Root (Magisk, SuperSU) ซึ่งไม่แนะนำสำหรับแอปทั่วไป
เมธอด Environment ที่เกี่ยวข้อง
// System Root (/system) File rootDir = Environment.getRootDirectory(); // System Data (/data) File dataDir = Environment.getDataDirectory(); // Legacy External Storage (Deprecated API 29+) File externalDir = Environment.getExternalStorageDirectory(); // App-specific (แนะนำให้ใช้ Context) File appFiles = context.getFilesDir(); // /data/data/pkg/files File appCache = context.getCacheDir(); // /data/data/pkg/cache File extFiles = context.getExternalFilesDir(null); // /sdcard/Android/data/pkg/files
สรุป
Environment.getRootDirectory() ใช้สำหรับดูข้อมูลระบบ (System Partition) ไม่ใช่สำหรับเก็บข้อมูลแอป สำหรับการพัฒนาแอป ควรใช้ Context Methods (getFilesDir(), getCacheDir(), getExternalFilesDir()) ที่ปลอดภัยและรองรับ Scoped Storage