1 year ago

#319280

test-img

Nicholas Bissessar

(Kotlin) How to write to a file in android studio?

I'm trying to write to a file that I created in my app but when I open the file, there's nothing in it. I tried several methods so far and this is the latest attempt at trying to write to a file. I was using the fileoutputstream before but the file was getting written to internal storage which is not viewable in the File Manager on the device. Am I doing something wrong in my implementation?

        //Function call for creating and generating the python code
        generate.setOnClickListener{
            //Getting Storage Permissions
            checkPermissions(Manifest.permission.READ_EXTERNAL_STORAGE, STORAGE_PERMISSION_CODE)
            checkPermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE, STORAGE_PERMISSION_CODE)

            val file = "file.txt"
            var path = getExternalFilesDir("Written Files")
            var fileOut = File(path, file)
            fileOut.delete()
            
            try
            {

                fileOut.createNewFile()                
                PrintWriter(openFileOutput(file, Context.MODE_PRIVATE)).use{
                        it.println("Hello World!")
                    }
            }
            //Catching any file errors that could occur
            catch(e: FileNotFoundException)
            {
                e.printStackTrace()
            }
            catch(e:NumberFormatException)
            {
                e.printStackTrace()
            }
            catch(e: IOException)
            {
                e.printStackTrace()
            }
            catch(e:Exception)
            {
                e.printStackTrace()
            }
            //Creating display message when generating the code
            Toast.makeText(this, "Generating", Toast.LENGTH_SHORT).show()

        }

Permission Functions

    private fun checkPermissions(permission:String, requestCode:Int)
    {
        if(ContextCompat.checkSelfPermission(this@MainActivity, permission)==PackageManager.PERMISSION_DENIED)
        {
            //Get Permission
            ActivityCompat.requestPermissions(this@MainActivity, arrayOf(permission), requestCode)

        }
        else
        {
            Toast.makeText(this@MainActivity, "Permission Already Granted", Toast.LENGTH_LONG)
        }
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)

        if(requestCode==CAMERA_PERMISSION_CODE)
        {
            if(grantResults.isNotEmpty() && grantResults[0]==PackageManager.PERMISSION_GRANTED)
            {
                Toast.makeText(this@MainActivity, "Camera Permission Granted", Toast.LENGTH_SHORT)
            }
            else
            {
                Toast.makeText(this@MainActivity, "Camera Permission Denied", Toast.LENGTH_SHORT)
            }
        }
        else if (requestCode==STORAGE_PERMISSION_CODE)
        {
            if(grantResults.isNotEmpty() && grantResults[0]==PackageManager.PERMISSION_GRANTED)
            {
                Toast.makeText(this@MainActivity, "Storage Permission Granted", Toast.LENGTH_SHORT)
            }
            else
            {
                Toast.makeText(this@MainActivity, "Storage Permission Denied", Toast.LENGTH_SHORT)
            }
        }
    }
}

android

kotlin

fileoutputstream

0 Answers

Your Answer

Accepted video resources