こちらと同じことを行いました。
[Android & Kotlin] アプリ固有の内部ストレージのファイル保存
プロジェクトの作成
プロジェクト名: file01
環境
ViewBinding を使います
app/build.gradle.kts
(省略)
android {
(省略)
buildFeatures{
viewBinding = true
(省略)
res/values/strings.xml
<resources>
<string name="app_name">file01</string>
<string name="hint">文字を入力</string>
<string name="save_file">Save file</string>
<string name="read_file">Read file</string>
</resources>
画面
layer/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="50dp"
android:autofillHints="@string/hint"
android:background="#fff"
android:hint="@string/hint"
android:inputType="text"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.05" />
<Button
android:id="@+id/button_save"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/save_file"
android:textSize="20sp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/button_read"
app:layout_constraintTop_toBottomOf="@+id/text_view"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/button_read"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/read_file"
android:textSize="20sp"
android:layout_marginEnd="20dp"
android:layout_marginTop="20dp"
app:layout_constraintLeft_toRightOf="@+id/button_save"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text_view" />
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textColor="#000"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.4" />
</androidx.constraintlayout.widget.ConstraintLayout>
プログラム
MainActivity.kt
package com.example.file01
import android.os.Bundle
import android.view.View
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import com.example.file01.databinding.ActivityMainBinding
import java.io.BufferedReader
import java.io.File
import java.io.FileReader
import java.io.FileWriter
import java.io.IOException
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private var file: File? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val context = applicationContext
val fileName = "TestFile.txt"
file = File(context.filesDir, fileName)
val buttonSave = findViewById<Button>(R.id.button_save)
// lambda
buttonSave.setOnClickListener {
// エディットテキストのテキストを取得
val text = binding.editText.text.toString()
saveFile(text)
if (text.length == 0) {
binding.textView.text = "No text !"
} else {
binding.textView.text = "Saved"
}
}
val buttonRead = findViewById<Button>(R.id.button_read)
// lambda
buttonRead.setOnClickListener { v: View? ->
val str = readFile()
if (str != null) {
binding.textView.text = str
} else {
binding.textView.text = "Reaf file error !"
}
}
}
// ファイルを保存
fun saveFile(str: String?) {
// try-with-resources
try {
FileWriter(file).use { writer -> writer.write(str) }
} catch (e: IOException) {
e.printStackTrace()
}
}
// ファイルを読み出し
fun readFile(): String? {
var text: String? = null
// try-with-resources
try {
BufferedReader(FileReader(file)).use { br -> text = br.readLine() }
} catch (e: IOException) {
e.printStackTrace()
}
return text
}
}
実行結果
Device Explorer で TestFile.txt が出来ていることを確認
View -> Tool Windows -> Device Explorer
data -> data -> com.example.file01
TestFile.txt をダブルクリックして内容を表示
この場合は、 Good Afternoon
adb で確認
adb -s emulator-5554 shell
$ adb -s emulator-5554 shell
emu64xa:/ $ su
emu64xa:/ # cd data/data/com.example.file01/files/
emu64xa:/data/data/com.example.file01/files # ls
TestFile.txt profileInstalled