将iText创建的PDF保存为内部存储器,为MODE_WORLD_READABLE [英] Save iText created PDF to internal memory as MODE_WORLD_READABLE
问题描述
文件生成正常。我决定添加一些逻辑来处理设备是否没有外部存储器,或者外部存储器是否设置为只读;在这种情况下,我会尝试将文件保存到我的应用程序的文件系统中的特定目录。这也很好。问题是当试图让第三方应用程序显示该文件时。我知道我应该将文件保存为Mode_World_Readable,但是无法通过我找到的示例将其保存。
The file is generating fine. I decided to add some logic to handle if the device didn't have an external storage, or if the external storage was set to read-only; in which case, I would try to save the file to a specific directory in my app's filesystem. This works fine, too. The problem is when trying to get the third party app to show the file. I understand that I should be saving the file as Mode_World_Readable, but just can't get it to work from the examples I have found.
以下是我的代码的摘录:
Here is an excerpt of my code:
//Build a filename from the data provided
String reportTitle = args.getFromDate() + "_" + args.getToDate() + ".pdf";
: : : :
: : : :
} else {
//Something else is wrong.
//Maybe there isn't a SD Card
//It may be one of many other states, but all we need
//to know is we can neither read nor write to external storage
mExternalStorageAvailable = mExternalStorageWriteable = false;
try {
//Get the absolute path to the filesystem directory
//where the internal files are saved.
File painReportFolder = ctx.getFilesDir();
//The string of the sub directory we want to access
String filename = "Reports/PainReports/";
//Create a new 'file' to build the directory
File file = new File(painReportFolder, filename);
//Make the directory if it doesn't exist
file.mkdirs();
//Build a FileOutputStream with the filename appended
theFile = new FileOutputStream(file + java.io.File.separator + reportTitle);
//This is what the examples suggest,
//But it doesn't work.
//In this configuration, at least
//FileOutputStream fos = openFileOutput(filename, Context.MODE_WORLD_READABLE);
: : : :
: : : :
// creation of the different writers
PdfWriter writer;
if(mExternalStorageAvailable == false && mExternalStorageWriteable == false){
//if no sd card, or sd card is read-only,
//write to specific directory in internal
writer = PdfWriter.getInstance(document, theFile );
}else{
//otherwise, write to a dir on the sd card
writer = PdfWriter.getInstance(document, new FileOutputStream(currentDirectory + java.io.File.separator + reportTitle));
}
所以PdfWriter.getInstance()会接受FileOutputStream,但它不会接受由openFileOutput创建的FileOutputStream我无法将MODE_WORLD_READABLE添加到'theFile'
So PdfWriter.getInstance() will accept a FileOutputStream, but it wont accept the FileOutputStream created by openFileOutput and I can't add MODE_WORLD_READABLE to 'theFile'
我在过去一周一直在研究这个问题,并尝试不同的代码变体。我在Stackoverflow或其他任何地方找到的任何内容都没有直接回复我的查询。
I've been researching this for the past week, and trying out different variations of code. Nothing I've found on Stackoverflow or anywhere else directly answers my query.
推荐答案
不知道为什么这是downvoted,或者由谁(可能是一些巨魔,因为没有留下任何评论),但进一步的研究表明我没有解决方案。
Don't know why this was downvoted, or by whom (probably some troll, as no comment was left), but further research has shown to me that there is no solution.
为什么?
取出你的SD卡并尝试拍照;它说它需要一张卡片 - 它不会保存到内部;这是一个捆绑的应用程序。
Take out your sd card and try to take a photo; it says that it needs a card - it wont save to internal; and this is a bundled app.
所以我回滚了所有的sd检查代码:
So I've rolled-back all of my sd checking code:
如果有sd;保存
如果有sd,但它是只读的;显示错误页面
If there is sd, but it is read-only; show error page
如果没有sd;显示错误页面
If there is no sd; show error page
这篇关于将iText创建的PDF保存为内部存储器,为MODE_WORLD_READABLE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!