如何通过Intnet发送从base64转换的PDF? [英] How to send PDF converted from base64 through Intnet?

查看:99
本文介绍了如何通过Intnet发送从base64转换的PDF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've got the pdf file in base64, I decoded it, send through the Intent but unfortunately external apps can't read it (Evernote e.g. says that it can't open empty file). Another strange thing is that I have no such apps like Adobe Reader or Messanger in the list after conversion. This is my code:

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();

我也尝试在线转换器检查转换是否正常.我有这样的东西:

I also try the online converter to check if the conversion went OK. I've got something like this:

%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

为避免诸如这是一张图片,为什么不以图片形式发送"之类的答案?我可以告诉你,这只是我转换的另一种选择.我的转换和将其共享为图像非常有效.感谢您的时间和任何帮助!

To avoid answers like "this is an image, why don't you send it as image" I can tell you that it is just another option for my conversion. My conversion and sharing it as image works perfectly. Thanks for your time and for any help!

@EDIT

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

I changed this line: File newFile = new File(filePath, "application/pdf"); to this: File newFile = new File(filePath, "doc.pdf");

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

But now I got the blank page in pdf.

推荐答案

错误#1:您打开一个文件,然后尝试将其交给另一个应用程序,然后再使用该文件(flush(),丢失的close().

Bug #1: You open a file, then attempt to hand it to another app before completing your work with the file (flush(), the missing getFD().sync(), and close().

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

Bug #2: You are doing disk I/O on the main application thread.

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

Bug #3: You are attempting to read the entire decoded PDF into heap space, which will fail for many PDF files, as they will be too large.

错误#4:newFile不会指向您尝试创建的文件,而是指向路径中带有application/pdf的文件.结果,另一个应用程序无法访问您的PDF,即使它已完全写入磁盘.

Bug #4: newFile does not point to the file that you are attempting to create, but rather some file with application/pdf in the path. As a result, the other app has no way to access your PDF, even if it were completely written to disk.

错误#5:您在Intent上没有FLAG_GRANT_READ_URI_PERMISSION,因此第三方应用可能无法读取您的内容(请参见

Bug #5: You do not have FLAG_GRANT_READ_URI_PERMISSION on the Intent, and so the third-party app may not have read access to your content (see the third bullet in the docs).

这篇关于如何通过Intnet发送从base64转换的PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆