ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android-从Base64中的给定URI将Dropbox上的PDF下载到电话中会导致无法读取的PDF损坏

2019-12-11 00:24:54  阅读:248  来源: 互联网

标签:dropbox android


我从不同的来源(电话,Google驱动器等本地)获取PDF的URI,对于Dropbox,我可以使用URI作为输入来读取字节数组.但是我得到的PDF不是有效的PDF. Base64也不正确.

这是我的URI:

内容://com.dropbox.android.FileCache/filecache/a54cc030-e2e0-4ef5-8e72-0ac3269a16e1

val inputStream = context.contentResolver.openInputStream(Uri.parse(uri))
val allText = inputStream.bufferedReader().use(BufferedReader::readText)
val base64Image = Base64.encodeToString(allText.toByteArray(), Base64.DEFAULT)

allText内容(摘要):

%PDF-1.3
%���������
4 0 obj
<< /Length 5 0 R /Filter /FlateDecode >>
.
.
.
13025
%%EOF

当存储带有.PDF扩展名的allText内容时,该功能不起作用.

格式看起来不错,但是当在https://base64.guru/converter/decode/pdf中插入base64Image时,表明它是不正确的.

PDF原始内容(摘要):

2550 4446 2d31 2e33 0a25 c4e5 f2e5 eba7
f3a0 d0c4 c60a 3420 3020 6f62 6a0a 3c3c
.
.
.
.
0a73 7461 7274 7872 6566 0a31 3330 3235
0a25 2545 4f46 0a

解决方法:

  • “I can read a byte array using the URI as input. But the PDF that I’m getting is not a valid PDF.”

  • “When storing the allText content with .PDF extension doesn’t work.”

您正在读取PDF输入字节(十六进制)并将其存储为错误的格式(文本).
例如,所有有效的PDF文件都应以字节25 50 44 46开头.您的allText内容片段以%PDF开头,%PDF是这些字节的转换后的ASCII / UTF文本表示形式.

问题:
所有这些都很好,因为我们可以将文本字符转换回各自的字节值,对吗?不,并非所有字节值都可以从文本格式正确恢复.

范例1:可以转换…

input bytes : 25 50 44 46
as text     : %  P  D  F
into bytes  : 25 50 44 46

示例#2:无法转换(由于这些字节没有文本字符,因此无法恢复原始数据)…

input bytes : 25 C4 E5 F2 E5 EB A7 F3 A0 D0
as text     : %  � � � �  � � � � � 
into bytes  : 25 00 00 00 00 00 00 00 00 00

解:

尝试以下类似的方法.您需要逻辑,如代码注释中所述…

import java.io.File
import java.io.InputStream

fun main(args: Array<String>) 
{
    //# setup access to your file...
    var inFile :InputStream = File("your-file-path-here.pdf")
    var fileSize :Int = File(path).length()

    //# read file bytes into a bytes Array...
    var inStream :InputStream = inFile.inputStream()
    var inBytes :ByteArray = inStream.readBytes()

    //# Make as String (of hex values)...
    //var hexString :String = ""
    val hexString = ""
    for (b in inBytes) { hexString = String.format("%02X", b) }

    //# check values as hex... should print: 25 
    //print(hexString) //could be long print-out for a big file

    //# Make Base64 string...
    val base64 = Base64.getEncoder().encodeToString(inBytes)
}

“Base64 is also not correct.”

(选项1)

尝试将上述示例代码中的hexString转换为Base64(注意:现在已添加为val base64).

(选项2)

使用简单的…直接将文件字节读取为Base64字符串

val bytes = File(filePath).readBytes()
val base64 = Base64.getEncoder().encodeToString(bytes)

标签:dropbox,android
来源: https://codeday.me/bug/20191210/2105186.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有