2012-10-02 73 views
0

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

回答

1

不知道爲什麼這是downvoted,或由誰(可能一些巨魔,因爲沒有評論留下),但進一步的研究表明,沒有解決方案。

爲什麼?

拿出你的SD卡,並嘗試拍照;它說它需要一張卡 - 它不會保存到內部;這是一個捆綁的應用程序。

所以我已經回滾了我所有的SD校驗碼:

如果有SD;保存

如果有sd,但它是隻讀的;顯示錯誤頁面

如果沒有sd;顯示錯誤頁面

相關問題