将iText创建的PDF作为MODE_WORLD_READABLE保存到内部存储器

时间:2012-10-02 19:48:37

标签: android itext

文件生成正常。我决定添加一些逻辑来处理设备是否没有外部存储器,或者外部存储器是否设置为只读;在这种情况下,我会尝试将文件保存到我的应用程序的文件系统中的特定目录。这也很好。问题是当试图让第三方应用程序显示该文件时。我知道我应该将文件保存为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 个答案:

答案 0 :(得分:1)

不知道为什么会被投票,或者由谁(可能是一些巨魔,因为没有留下任何评论),但进一步的研究向我显示没有解决方案。

为什么?

取出你的SD卡并尝试拍照;它说它需要一张卡片 - 它不会保存到内部;这是一个捆绑的应用程序。

所以我已经回滚了所有的sd检查代码:

如果有sd;保存

如果有sd,但它是只读的;显示错误页面

如果没有sd;显示错误页面

相关问题