ファイルは正常に生成されています。デバイスに外部ストレージがない場合、または外部ストレージが読み取り専用に設定されている場合に処理するロジックを追加することにしました。その場合、アプリのファイル システムの特定のディレクトリにファイルを保存しようとします。これもうまくいきます。問題は、サードパーティのアプリにファイルを表示させようとするときです。ファイルを 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 を受け入れず、「theFile」に MODE_WORLD_READABLE を追加できません。
私はこの 1 週間、これを調査し、さまざまなコードのバリエーションを試してきました。Stackoverflow や他の場所で見つけたもので、クエリに直接答えるものはありません。