Cannot write files in cache directory

Viewed 211

I'm not able to write files in cache directory

val file = File(context.cacheDir, usbFile.name)
val bufferedOutputStream = context.openFileOutput(usbFile.name, Context.MODE_PRIVATE).buffered()
val bufferedInoutStream = UsbFileInputStream(usbFile).buffered()

var read = 0
while ({ read = bufferedInoutStream.read(); read != -1 }()) {
    bufferedOutputStream.write(read)
}

it works fine using context.filesDir but not context.cacheDir.. I'm using libaums Library to read USB files.

1 Answers

openFileOutput() writes to the filesystem location where getFilesDir() points. openFileOutput() is unrelated to getCacheDir().

So, if you want to write to getCacheDir(), since you already have file pointing there, replace:

val bufferedOutputStream = context.openFileOutput(usbFile.name, Context.MODE_PRIVATE).buffered()

with:

val bufferedOutputStream = FileOutputStream(file).buffered()
Related