So I started with creating my own directory under /cache
folder.
fun getDirectory(context: Context): File? {
val directory = File(context.cacheDir, X_DIRECTORY)
if (!directory.exists()) {
if (!directory.mkdir()) {
return null
}
}
return directory
}
Then I download file using DownloadManager
and saved the file in /Downloads
external folder.
Lastly, I try to copy newly download file from /Downloads
to /cache/my_directory
/**
* copy file from source to destination
*
* @param src source
* @param dst destination
* @throws java.io.IOException in case of any problems
*/
@Throws(IOException::class)
fun copyFile(src: File?, dst: File?) {
try {
val inChannel = FileInputStream(src).channel
val outChannel = FileOutputStream(dst).channel
try {
inChannel.transferTo(0, inChannel.size(), outChannel)
} finally {
inChannel.close()
outChannel.close()
}
} catch (e: FileNotFoundException) {
Timber.e(e)
}
}
Where src = /storage/emulated/0/Download/downloaded_file.png
and dst = /data/user/0/.../cache/my_directory
.
What I receive is following exception: java.io.FileNotFoundException: /data/user/0/.../cache/my_directory: open failed: EISDIR (Is a directory)