0

Here's what I am trying to do: I want to create a file in /storage/emulated/0 called meks-balance.txt and write 100 to it if it doesn't exists.
I have included needed permissions in AndroidManifest.\

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

Here's my code:

package com.milk.meks

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import java.io.File

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val balanceFile = File("/storage/emulated/0/meks-balance.txt")
        if(!balanceFile.exists()){
            balanceFile.writeBytes(ByteArray(size = 0))
            balanceFile.writeText(text = "100")
        }
    }
}
``
  • Think that it has nothing to do with Kotlin but you using an Android 10+ device. – blackapps Oct 10 '20 at 10:59
  • "I have included needed permissions in AndroidManifest." -- that is insufficient, and has been for several years. You need to request the permission at runtime. And, as blackapps indicated, there are limits on where you can write on Android 10+ by default even with those permissions: https://stackoverflow.com/a/64119463/115145 – CommonsWare Oct 10 '20 at 11:08
  • So, CommonsWare, how to do that? – Молоко Молочко Oct 10 '20 at 11:28

0 Answers0