ภาพรวม
ตัวอย่างนี้สาธิตวิธีสร้างไฟล์ข้อความ (txt) และอ่านไฟล์จาก Internal Storage ในแอปพลิเคชัน Android
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
เปิด Android Studio ไปที่ File → New Project และกรอกรายละเอียดที่จำเป็น
ขั้นตอนที่ 2: ออกแบบ Layout (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"
tools:context=".MainActivity"
android:orientation="vertical">
<EditText
android:id="@+id/enterText"
android:hint="Please enter text here"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/save"
android:text="Save"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/output"
android:layout_width="wrap_content"
android:textSize="25sp"
android:layout_height="wrap_content" />
</LinearLayout>
Layout ประกอบด้วย EditText สำหรับป้อนข้อความ ปุ่ม Save สำหรับบันทึก และ TextView สำหรับแสดงผลข้อมูลที่อ่านกลับมา
ขั้นตอนที่ 3: เขียน MainActivity.java
package com.example.andy.myapplication;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
public class MainActivity extends AppCompatActivity {
Button save;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView output = findViewById(R.id.output);
final EditText enterText = findViewById(R.id.enterText);
save = findViewById(R.id.save);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!enterText.getText().toString().isEmpty()) {
// สร้างโฟลเดอร์ text ใน Internal Storage
File file = new File(MainActivity.this.getFilesDir(), "text");
if (!file.exists()) {
file.mkdir();
}
try {
// สร้างไฟล์ sample.txt
File gpxfile = new File(file, "sample");
FileWriter writer = new FileWriter(gpxfile);
writer.append(enterText.getText().toString());
writer.flush();
writer.close();
// อ่านไฟล์และแสดงผล
output.setText(readFile());
Toast.makeText(MainActivity.this, "Saved your text", Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
}
private String readFile() {
File fileEvents = new File(MainActivity.this.getFilesDir() + "/text/sample");
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(fileEvents));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
return text.toString();
}
}
ขั้นตอนที่ 4: กำหนดสิทธิ์ใน AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.andy.myapplication">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<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>
หมายเหตุ: สำหรับ Internal Storage จริงๆ แล้วไม่ต้องการ permission WRITE_EXTERNAL_STORAGE และ READ_EXTERNAL_STORAGE แต่โค้ดตัวอย่างนี้ใส่ไว้เพื่อความเข้ากันได้กับ External Storage ด้วย
การทดสอบและผลลัพธ์
รันแอปพลิเคชัน ป้อนข้อความในช่อง EditText แล้วกด Save


เพื่อตรวจสอบ ไฟล์จะถูกจัดเก็บที่เส้นทาง: /data/data/<your.package.name>/files/text/sample.txt

สรุป
Internal Storage เหมาะสำหรับจัดเก็บข้อมูลส่วนตัวของแอป ไม่ต้องการ Runtime Permission (Android 6.0+) และข้อมูลจะถูกลบเมื่อผู้ใช้ลบแอปออก