Saving Files

Useful for saving bitmaps and other media.

Two types of storage:

  • Internal

  • external

Internal

getFilesDir() returns a File representing the internal directory for your app getCacheDir() returns a FIle representing the internal Cache directory

val file = File(context.filesDir, filename)


val filename = "myfile"
val string = "Hello world!"
context.openFileOutput(filename, Context.MODE_PRIVATE).use {
      it.write(string.toByteArray())
}


fun getTempFile(context: Context, url: String): File? =
        Uri.parse(url)?.lastPathSegment?.let { filename ->
            File.createTempFile(filename, null, context.cacheDir)
        }

Delete a file

myFile.delete()
myContext.deleteFile(fileName)

results matching ""

    No results matching ""