【发布时间】:2017-09-08 11:35:20
【问题描述】:
我在base64 中有pdf 文件,我对其进行了解码,通过Intent 发送但不幸的是外部应用程序无法读取它(Evernote 例如说它可以'不打开空文件)。另一个奇怪的是,我在转换后的列表中没有像 Adobe 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
为了避免“这是一张图片,你为什么不把它作为图片发送”之类的答案我可以告诉你,这只是我转换的另一种选择。我的转换和作为图像共享它完美无缺。感谢您的时间和任何帮助!
@EDIT
我改变了这一行:
File newFile = new File(filePath, "application/pdf");
对此:
File newFile = new File(filePath, "doc.pdf");
但现在我得到了pdf格式的空白页。
【问题讨论】:
-
确保文件保存正确,如果没有其他可用的外部应用程序,请尝试在 webview 中加载文件。
-
@SARATHV 好吧,正如我所说,有可用的应用程序:Evernote 或 Gmail。他们说:“无法读取空文件”。你读过这篇文章吗?
-
@SARATHV 我为图像导出做了同样的保存,它可以工作,为什么它在这里会出错?
标签: android pdf android-intent base64