如何发送从base64通过Intnet转换的PDF?

时间:2017-09-08 11:35:21

标签: android pdf android-intent base64

我在pdf中获得了base64文件,我对其进行了解码,通过Intent发送,但遗憾的是外部应用无法读取它( Evernote 例如说它不能打开空文件)。另一个奇怪的事情是我在转换后的列表中没有像 Adob​​e Reader Messanger 这样的应用程序。这是我的代码:

FileOutputStream fos;
fos = context.openFileOutput("doc.pdf", Context.MODE_PRIVATE);
String fileB64 = url.split(",")[1];
byte[] decodedStr = Base64.decode(fileB64, Base64.NO_WRAP);
fos.write(decodedStr);
Intent sendIntent = new Intent();
sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
sendIntent.setAction(Intent.ACTION_SEND);
File filePath = new File(String.valueOf(context.getFilesDir()));
File newFile = new File(filePath, "application/pdf");
sendIntent.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".FileProvider", newFile));
sendIntent.setType("application/pdf");
context.startActivity(sendIntent);
fos.flush();
fos.close();

我也尝试在线转换器检查转换是否正常。我有这样的事情:

%PDF-1.3
3 0 obj
<</Type /Page
/Parent 1 0 R
/Resources 2 0 R
/MediaBox [0 0 348.00 256.00]
/Contents 4 0 R
>>
endobj
4 0 obj
<</Length 11287>>
stream
1.000 0.000 0.000 -1.000 0.000 256.000 cm
0.20 w
0 G
q
1.000 0.000 0.000 1.000 0.000 0.000 cm
q
/F9 16 Tf
/F9 12 Tf
q
q
Q
q
Q
Q
q
Q
q
/GS1 gs
1.000 0.000 0.000 1.000 0.000 0.000 cm
0.000 0.000 m 
348.000 0.000 l
348.000 0.000 348.000 0.000 348.000 0.000 c
348.000 256.000 l
348.000 256.000 348.000 256.000 348.000 256.000 c
0.000 256.000 l

要避免像&#34这样的答案;这是一张图片,为什么不把它作为图片发送?我可以告诉你,这只是我转换的另一种选择。我的转换和分享图像完美无缺。感谢您的时间和任何帮助!

@EDIT

我改变了这一行:  File newFile = new File(filePath, "application/pdf"); 对此:  File newFile = new File(filePath, "doc.pdf");

但现在我得到了pdf的空白页。

1 个答案:

答案 0 :(得分:1)

错误#1:您打开一个文件,然后尝试将其交给另一个应用,然后再完成对该文件的处理(flush(),丢失的getFD().sync()close()。< / p>

错误#2:您正在主应用程序线程上执行磁盘I / O.

错误#3:您正在尝试将整个已解码的PDF读入堆空间,这对于许多PDF文件来说都会失败,因为它们太大了。

错误#4:newFile未指向您尝试创建的文件,而是指向路径中包含application/pdf的文件。因此,其他应用无法访问您的PDF,即使它已完全写入磁盘。

错误#5:FLAG_GRANT_READ_URI_PERMISSION上没有Intent,因此第三方应用可能对您的内容没有读取权限(请参阅the docs中的第三个项目符号)。

相关问题