Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 试图将base64字符串解码为PDF,但该文件始终损坏。我做错了什么?_Java_Android_Kotlin - Fatal编程技术网

Java 试图将base64字符串解码为PDF,但该文件始终损坏。我做错了什么?

Java 试图将base64字符串解码为PDF,但该文件始终损坏。我做错了什么?,java,android,kotlin,Java,Android,Kotlin,我有一个使用base64编码的pdf。我正在尝试将这个编码字符串解码,并将其保存为pdf格式的手机。文件按预期保存,但无法打开,因为它已损坏。我尝试了不同的PDF,但仍然得到相同的结果。这是我的密码 val cachePath = File(context.cacheDir, "pdfs") cachePath.mkdirs() val stream = FileOutputStream(cachePath.path + "/$PATH_NAME") stream.write(Base64.d

我有一个使用base64编码的pdf。我正在尝试将这个编码字符串解码,并将其保存为pdf格式的手机。文件按预期保存,但无法打开,因为它已损坏。我尝试了不同的PDF,但仍然得到相同的结果。这是我的密码

val cachePath = File(context.cacheDir, "pdfs")
cachePath.mkdirs()

val stream = FileOutputStream(cachePath.path + "/$PATH_NAME")
stream.write(Base64.decode(inputString, Base64.DEFAULT))
stream.flush()
stream.close()

val file = File(cachePath, PATH_NAME)
FileProvider.getUriForFile(context,"file_provider_authority",file)?.let {  uri ->
    val intent = Intent(Intent.ACTION_CREATE_DOCUMENT)
    with(intent) {
        flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
        flags = Intent.FLAG_ACTIVITY_NO_HISTORY
        data = (uri)
        type = context.contentResolver.getType(uri)
    }
    startActivity(Intent.createChooser(intent, "PDF"))
}