1

「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;

   }

ありがとうございました!

4

2 に答える 2

1

アセットからの入力ストリームとしてアクセスするには:

in = new BufferedReader(new InputStreamReader(activity.getAssets().open(yourfile.pdf)));
于 2012-06-13T17:53:12.070 に答える
1

何時間にもわたって、かなりの数のたばこ休憩の後、ここで解決策です。readToByteBuffer が ByteBuffer を返したら、ByteBuffer を受け取る新しい PDFFile を作成するのと同じくらい簡単です。

楽しみ...

ShowPDF ボタン リスナー...

OnClickListener ShowPdfListener = new OnClickListener() {
    public void onClick(View v)
    {
        Intent intent = new Intent(PdfFileSelectActivity.this,
        PdfViewerActivity.class);
        startActivity(intent);
    }
};

onCreate() PdfViewerActivity で...

openFile2(readToByteBuffer(this.getAssets().open("test.pdf")), null);

ここから readToByteBuffer() を編集

public ByteBuffer readToByteBuffer(InputStream inStream) throws IOException
{
    long startTime = System.currentTimeMillis();
    BufferedReader in = new BufferedReader(new InputStreamReader(this.getAssets().open("test.pdf")));
    StringBuilder total = new StringBuilder();
    String line;
    while ((line = in.readLine()) != null) {
        total.append(line);
    }

    int length = total.length();
    byte[] buffer = new byte[length];
    ByteArrayOutputStream outStream = new ByteArrayOutputStream(length);
    int read;
    while (true) {
      read = inStream.read(buffer);
      if (read == -1)
        break;
      outStream.write(buffer, 0, read);
    }
    ByteBuffer byteData = ByteBuffer.wrap(outStream.toByteArray());
    long stopTime = System.currentTimeMillis();
    mGraphView.fileMillis = stopTime-startTime;
    return byteData;
  }
于 2012-06-13T20:02:03.970 に答える