Skip to content

Moster 05 Read and Write files#5

Merged
AnelCC merged 5 commits intomasterfrom
Moster_05_Read_And_Write_Files
Apr 20, 2020
Merged

Moster 05 Read and Write files#5
AnelCC merged 5 commits intomasterfrom
Moster_05_Read_And_Write_Files

Conversation

@AnelCC
Copy link
Copy Markdown
Owner

@AnelCC AnelCC commented Apr 20, 2020

Moster 05 Read and Write files

1. Save files to internal storage.

We using app.cacheDir for get the cache's address from the device.

      fun saveTextToFile(app: Application, json: String?) {
            val file = File(app.cacheDir, "monsters.json")
            file.writeText(json ?: "", Charsets.UTF_8)
        }

2. Read files from internal storage.

We using app.cacheDir for get the cache's address from the device and find the info saved before so we can read

        fun readTextFile(app: Application): String? {
            val file = File(app.cacheDir, "monsters.json")
            return if (file.exists()) {file.readText()} else null
        }

3. Manage permissions in a splash screen.

splash_permision_denied

4. Request external storage permissions.

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

splash_request_permision ....................... splash_permision_denied

5. Save files to external storage

      fun saveTextToFile(app: Application, json: String?) {
            val file = File(app.getExternalFilesDir("monsters"), "monsters.json")
            file.writeText(json ?: "", Charsets.UTF_8)
        }

        fun readTextFile(app: Application): String? {
            val file = File(app.getExternalFilesDir("monsters"), "monsters.json")
            return if (file.exists()) {file.readText()} else null
        }

@AnelCC AnelCC merged commit 1070ef7 into master Apr 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant