「res/raw」またはassetsフォルダーからプログラムでPDFファイルにアクセスして、指定されたメソッドで解析します
説明:
現在、このプログラムは、選択されたファイル パスを取得し、それを「mFilename」EditText フィールドに設定するファイル マネージャからファイルにアクセスします。以下の [PDF を表示] ボタン リスナーは、文字列 'pdffilename' が 'mFilename' EditText フィールドに含まれる文字列に割り当てられることを示しています。PdfViewerActivity が開始され、文字列 'pdffilename' が Extra として渡されます。onCreate() では、意図が null かどうかがチェックされます。これは、変更を行うことができる/行う必要があると私が思うところです。文字列「pdffilename」には、以下に示すものが割り当てられます。私がしたいのは、PDFファイルを2つの方法のいずれかで保存することです... int 'res/raw/example_folder/example.pdf'またはassetsフォルダーに。これらのPDFファイルを保存している場所のパスをプログラムで「pdffilename」に割り当てたいと思います。
だから基本的に...
- PDF ファイルを「res/raw/folder_example/example.pdf」または assets フォルダーに保存したい
- ファイルマネージャーを使用する必要がないため、コードからこれらのファイルにアクセスしたい
- とにかく、これが最大の助けになるでしょう。私はJavaが得意ですが、決してスーパースターではないので、コードで少し説明してください
コメントへの返信とこの投稿の編集をお待ちしております。この投稿が他のユーザーに役立つことを願っているので、解決策のコードを投稿します。完成時。ありがとうございました!
PdfFileSelectActivity で PDF ボタン リスナーを表示...
OnClickListener ShowPdfListener = new OnClickListener()
{
public void onClick(View v)
{
mFilename = (EditText) findViewById(R.id.filename);
String pdffilename = mFilename.getText().toString();
Intent intent = new Intent(PdfFileSelectActivity.this,
PdfViewerActivity.class)
.putExtra(EXTRA_PDFFILENAME, pdffilename);
startActivity(intent);
}
};
上記の show PDF Listener から呼び出される PdfViewerActivity の onCreate()...
Intent intent = getIntent();
if (intent != null)
{
if ("android.intent.action.VIEW".equals(intent.getAction()))
{
pdffilename = storeUriContentToFile(intent.getData());
}
else {
pdffilename = getIntent().getStringExtra(PdfFileSelectActivity.EXTRA_PDFFILENAME);
}
}
if (pdffilename == null)
pdffilename = "no file selected";
setContent(null);
上記から呼び出される setContent() (必要な場合) ...
private void setContent(String password)
{
try {
parsePDF(pdffilename, password);
}
catch (PDFAuthenticationFailureException e)
{
System.out.println("Password needed");
}
}
上記から呼び出される parsePDF() (必要な場合) ...
private void parsePDF(String filename, String password) throws PDFAuthenticationFailureException
{
long startTime = System.currentTimeMillis();
try {
File f = new File(filename);
long len = f.length();
if (len == 0) {
mGraphView.showText("file '" + filename + "' not found");
}
else {
mGraphView.showText("file '" + filename + "' has " + len + " bytes");
openFile(f, password);
}
}
catch (PDFAuthenticationFailureException e)
{
throw e;
} catch (Throwable e) {
e.printStackTrace();
mGraphView.showText("Exception: "+e.getMessage());
}
long stopTime = System.currentTimeMillis();
mGraphView.fileMillis = stopTime-startTime;
}
ありがとうございました!