2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

kotlinでandroid入門 内部ストレージ

Posted at

内部ストレージ

端末のアプリ専用のフォルダーにアクセス
ファイルとして扱えるんですが、他からアクセスできないのでなんか使いづらいかと。

1. 補足

アプリ専用のストレージ
他からアクセス出来ない

2. 書き込み

例はテキストファイル
localFileName=ファイル名

    val saveData : String = "ABCDEFG"
    try {
        // 追記書き込みでオープン (=Context.MODE_APPEND)
        val outputstream: FileOutputStream = openFileOutput(localFileName, Context.MODE_APPEND)
        outputstream.write(saveData.toByteArray())
        outputstream.write("\n".toByteArray())
        outputstream.close()
    } catch (e: IOException) {
        e.printStackTrace();
    }

3. 読込

    val dataList : ArrayList<String> = ArrayList<String>()
    try {
        val fis = openFileInput(localFileName)
        val reader = fis.bufferedReader()
        for (lineBuffer in reader.readLines()) {
            dataList.add(lineBuffer)
        }
        reader.close()
        fis.close()
    } catch (e: IOException) {
        e.printStackTrace()
    }

4. 削除

    try {
        deleteFile(localFileName)
    } catch ( e : IOException) {
        e.printStackTrace();
    }

これで一応アクセスできます

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?