該文件生成正常。我決定添加一些邏輯來處理設備是否沒有外部存儲,或者如果外部存儲設置爲只讀;在這種情況下,我會嘗試將文件保存到我應用程序文件系統中的特定目錄。這工作也很好。問題是當試圖讓第三方應用程序顯示該文件。我明白我應該將文件保存爲Mode_World_Readable,但是無法從我找到的示例中找到它。保存iText創建的PDF到內部存儲器作爲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或其他地方找到的直接回答我的查詢。