该文件生成良好。如果设备没有外部存储,或者外部存储设置为只读,我决定添加一些逻辑来处理;在这种情况下,我会尝试将文件保存到我的应用程序文件系统中的特定目录中。这也很好用。问题是在尝试让第三方应用程序显示文件时。我知道我应该将文件保存为 Mode_World_Readable,但只是无法从我找到的示例中使其正常工作。
这是我的代码的摘录:
//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”
过去一周我一直在研究这个,并尝试了不同的代码变体。我在 Stackoverflow 或其他任何地方都找不到直接回答我的查询的内容。