Anath43ema
Anath43ema

Reputation: 39

Cannot write files in cache directory

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.

Upvotes: 2

Views: 773

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006614

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()

Upvotes: 1

Related Questions