Kotlin write ByteArray to newly created File on USB Pendrive

Viewed 33

OS: Linux | Language: Kotlin | Android API 31

The overall Task is to write an Errorlog to a external USB Pendrive. I managed to gain access to the Pendrive by calling following Intent:

    fun gainPermission(){
    val intent = Intent()
    intent.action = Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION
    val uri = Uri.fromParts("package", this.javaClass.packageName, null)
    intent.setData(uri)
    startActivity(intent)
}

After that i create a new Directory on the Pendrive (Works fine). After that i create a File and try to write some Data to it.

fun writeToUsb(storageVolume: StorageVolume) {
        when(storageVolume.state) {
            envMounted -> {
                val rootDir = storageVolume.directory
                var myDir = File("$rootDir/errors")
                if (!myDir.exists()) {
                    try {
                        val created = myDir.mkdirs()
                        Log.i("createDir", "$created")
                    } catch (e: Exception) {
                        Log.i("Failure at creating the directory", "$e")
                    }
                    val dataToWrite = "FooBar".toByteArray()
                    var file = File(myDir, "errorlog.txt")
                    val fos = FileOutputStream(file)
                    Log.i("Permission to write: ", "${file.canWrite()}")
                    fos.write(dataToWrite)
                    fos.flush()
                    fos.close()
                }
            }
        }
}

After executing this code. I can see that the folder "errors" is created and inside there is a file called "errorlog.txt".

But the file does not contain any values. Its just an empty Textfile. I thought that i maybe do not have the permission to write to this File, so i checked with file.canWrite() --> it returns

2022-09-21 12:41:01.328 26020-26020/com.example.usbstickapp I/Permission to write:: true

Either way the file i create is empty and i cant find a solution to this. Does someone have a solution for this? Im not getting Errors or Exception by executing writeToUsb()

EDIT:

I just tested it with exactly this code

fun writeToUsb(storageVolume: StorageVolume) {
        when(storageVolume.state) {
            envMounted -> {
                val myRootDir = storageVolume.directory
                val myDir = File("$myRootDir/log")
                if(!myDir.exists()){
                    try {
                        val check = myDir.mkdirs()
                        Log.i("Create Dirs", "$check")
                    }catch (e: Exception){
                        Log.i("Create Dirs", "$e")
                    }
                }
                val fileName = "ErrorLog.txt"
                var file = File(myDir, fileName)
                val isCreated = file.createNewFile()
                Log.i("File created", "$isCreated")
                Log.i("File writable", "${file.canWrite()}")
                file.writeBytes("lkjfalksdjfadklsf".toByteArray())
                if(file.exists()){
                    file.delete()
                }
                val fos = FileOutputStream(file)
                fos.write("test123blabla".toByteArray())
                fos.flush()
                fos.close()


            }

and it works. In the File there was "test123balbla". I deleted the folders and tried again... Nothing not working anymore.

It seems like in some very rare conditions it works. I did not change anything and tried again but then suddently it doesnt work anymore. Maybe this helps

0 Answers
Related