Giới thiệu
Sử dụng Environment.getRootDirectory() để lấy thư mục gốc hệ thống (thường là /system).
Code (MainActivity.java)
public class MainActivity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.text);
File rootDir = Environment.getRootDirectory();
textView.setText("Root directory: " + rootDir.getAbsolutePath() +
"\nExists: " + rootDir.exists() +
"\nCan read: " + rootDir.canRead() +
"\nCan write: " + rootDir.canWrite());
}
}
Kết quả mẫu
Root directory: /system
Exists: true
Can read: true
Can write: false
Lưu ý
- Thư mục
/systemchứa OS, framework, app hệ thống. Chỉ đọc (read-only) trên thiết bị production. - Cần root (superuser) để ghi vào đây, không khuyến khích trên app bình thường.
- Dùng
Environment.isExternalStorageEmulated()để kiểm tra shared storage.