0

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)

MaaAn13
  • 264
  • 5
  • 24
  • 54
  • "Then I download file using DownloadManager and saved the file in /Downloads external folder" -- `DownloadManager` is no longer useful for your scenario. `DownloadManager` now is for cases where the user might need the downloaded content, but your app does not (think a browser downloading a file). If your app needs access to the downloaded content, download it yourself directly to your desired file, such as [via OkHttp](https://stackoverflow.com/a/29012988/115145). – CommonsWare Aug 12 '21 at 11:17
  • `dst = /data/user/0/.../cache/my_directory.` That is a directory path. But you should supply a file path like dst = /data/user/0/.../cache/my_directory/myfile.png – blackapps Aug 12 '21 at 11:38

0 Answers0