Android Studio: скопированный файл выдает исключение IllegalArgumentException: неверные аргументы при передаче в качестве аргумента - PullRequest
0 голосов
/ 18 июня 2020

Я пытаюсь зашифровать файл, выбранный из SAF. Из других ответов я узнал, что мне нужно сначала скопировать файл в каталог, прежде чем зашифровать файл. Это то, что я сделал, и нашел здесь метод копирования .

Итак, вот что у меня есть.

@Throws(IOException::class)
private fun execute(){
    val filepathTextView: TextView = findViewById(R.id.pathTextView)
    val filepath: String = filepathTextView.text.toString()
    val file = File(filepath)

    val copiedFile: File = File(getExternalFilesDir(null), filename + "copied")
    val inputStream: InputStream = contentResolver.openInputStream(tempURI)!!
    copyStreamToFile(inputStream, copiedFile)
    val existcheck: Boolean = copiedFile.exists()
    val readcheck: Boolean = copiedFile.canRead()

    encrypt(copiedFile)
}
// Got this from the link above.
fun copyStreamToFile(inputStream: InputStream, outputFile: File) {
    inputStream.use { input ->
        val outputStream = FileOutputStream(outputFile)
        outputStream.use { output ->
            val buffer = ByteArray(4 * 1024) // buffer size
            while (true) {
                val byteCount = input.read(buffer)
                if (byteCount < 0) break
                output.write(buffer, 0, byteCount)
            }
            output.flush()
        }
    }
}

encrypt(copiedFile) бросает "java.lang.IllegalArgumentException: Bad arguments", чего я не понимаю. encrypt() принимает File в качестве аргумента, и я проверил, что copiedFile exists() и canRead() возвращают true. Есть предложения?

enter image description here

...