本文介绍了如何为Base64转换成PDF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给定一个base64输入,我将如何将其转换为Android中的PDF文件?
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
上下文=这一点;
的setContentView(R.layout.activity_main);
//获取PDF文件EN code入的Base64
档案文件=新的文件(到/ mnt / SD卡/下载/ Base64.pdf);
尝试{
//文件是番石榴库
Files.toByteArray(文件);
// EN coded进入的Base64
字节[]临时= Base64.en code(Files.toByteArray(文件),Base64.DEFAULT);
//出于测试目的,写了EN codeD字符串文件
将writeToFile(Base64.en codeToString(温度,Base64.DEFAULT)的ToString()); }赶上(IOException异常五){
Log.d(标签,File.toByteArray()错误);
e.printStackTrace();
}
}
我能够使用来测试PDF文件被正确连接codeD。不过,我想能够去code为Base64回PDF自己。我会怎么做呢?
修改1
试过低于code。通过Saneesh CS的建议 @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
上下文=这一点;
的setContentView(R.layout.activity_main);
//获取PDF文件EN code入的Base64
档案文件=新的文件(到/ mnt / SD卡/下载/ Base64.pdf);
尝试{
//文件是番石榴库
Files.toByteArray(文件);
// EN coded进入的Base64
字节[]临时= Base64.en code(Files.toByteArray(文件),Base64.DEFAULT);
//与下面的线,但同样的结果尝试。
字节[] =的temp1 Base64.en code(Files.toByteArray(文件),0);
//出于测试目的,写了EN codeD字符串文件
//将writeToFile(Base64.en codeToString(温度,Base64.DEFAULT)的ToString());
最终文件dwldsPath =新的文件(Environment.getExternalStorageDirectory(),test7.pdf);
字节[] = pdfAsBytes Base64.de code(温度,0);
FileOutputStream中的操作系统;
OS =新的FileOutputStream(dwldsPath,FALSE);
os.write(pdfAsBytes);
os.flush();
os.close();
}赶上(IOException异常五){
Log.d(标签,File.toByteArray()错误);
e.printStackTrace();
}
}
以上code ++工程。
解决方案
最后文件dwldsPath =新的文件(DOWNLOADS_FOLDER +文件名+.PDF);
字节[] = pdfAsBytes Base64.de code(TXT,0);
FileOutputStream中的操作系统;
OS =新的FileOutputStream(dwldsPath,FALSE);
os.write(pdfAsBytes);
os.flush();
os.close();
试试这个code。 TXT是的base64 EN codeD字符串。
Given a base64 input, how would I convert it into a PDF file in Android?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
setContentView(R.layout.activity_main);
// Get the PDF file to encode it into Base64
File file = new File("/mnt/sdcard/download/Base64.pdf");
try {
// Files is from Guava library
Files.toByteArray(file);
// Encoded into Base64
byte[] temp = Base64.encode(Files.toByteArray(file), Base64.DEFAULT);
// For testing purposes, wrote the encoded string to file
writeToFile(Base64.encodeToString(temp, Base64.DEFAULT).toString());
} catch (IOException e) {
Log.d(tag, "File.toByteArray() error");
e.printStackTrace();
}
}
I was able to use http://www.webutils.pl/index.php?idx=base64 to test the PDF file was being encoded properly. But I would like to be able to decode the base64 back to PDF myself. How would I do that?
Edit 1
Tried the below code as suggested by Saneesh CS
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
setContentView(R.layout.activity_main);
// Get the PDF file to encode it into Base64
File file = new File("/mnt/sdcard/download/Base64.pdf");
try {
// Files is from Guava library
Files.toByteArray(file);
// Encoded into Base64
byte[] temp = Base64.encode(Files.toByteArray(file), Base64.DEFAULT);
// Tried with the below line, but same result.
byte[] temp1 = Base64.encode(Files.toByteArray(file), 0);
// For testing purposes, wrote the encoded string to file
// writeToFile(Base64.encodeToString(temp, Base64.DEFAULT).toString());
final File dwldsPath = new File(Environment.getExternalStorageDirectory(), "test7.pdf");
byte[] pdfAsBytes = Base64.decode(temp, 0);
FileOutputStream os;
os = new FileOutputStream(dwldsPath, false);
os.write(pdfAsBytes);
os.flush();
os.close();
} catch (IOException e) {
Log.d(tag, "File.toByteArray() error");
e.printStackTrace();
}
}
Above code works.
解决方案
final File dwldsPath = new File(DOWNLOADS_FOLDER + fileName + ".pdf");
byte[] pdfAsBytes = Base64.decode(txt, 0);
FileOutputStream os;
os = new FileOutputStream(dwldsPath, false);
os.write(pdfAsBytes);
os.flush();
os.close();
Try this code. txt is the base64 encoded string.
这篇关于如何为Base64转换成PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!