1

Access PDF files from 'res/raw' or assets folder programmatically to parse with given methods

Explanation:

Right now this program accesses a file from a file manager that takes the selected files path and sets it to the 'mFilename' EditText field. The show PDF button listener below shows the String 'pdffilename' gets assigned the String contained in the 'mFilename' EditText field. The PdfViewerActivity is started and the String 'pdffilename' is passed as an Extra. In the onCreate() the intent is checked if null or not. This is where I think the change can/should be made. The String 'pdffilename' is assigned what you see below. What I want to do is store the PDF files in one of two ways... int the 'res/raw/example_folder/example.pdf' or in the assets folder. I want to assign 'pdffilename' programmatically with the path for where I am storing these PDF files. I have tried many different approaches all of which have either did not compile, caused errors, or caused a "file: res/raw/example_folder/example.pdf does not exist!".

So Basically...

  • I want to store PDF files in 'res/raw/folder_example/example.pdf' or the assets folder
  • I want to access these files from the code as in I do not need to use a file manager
  • Anyway that will solve this would be the biggest help, I am pretty good with Java, but I am by no means a superstar so please explain a little with your code

Thank you so much and I will be standing by to answer comments and edit this post. I hope this post will be helpful to other users so I will be posting the code for the solution. when completed. Thank you again!

Show PDF button Listener in PdfFileSelectActivity...

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);
    }
};

PdfViewerActivity's onCreate() invoked from show PDF Listener above...

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() called from above (if needed) ...

private void setContent(String password)
{
    try {
        parsePDF(pdffilename, password);
    }
    catch (PDFAuthenticationFailureException e)
    {
        System.out.println("Password needed");
    }
}

parsePDF() called from above (if needed) ...

    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;

   }

Thank you again!

Community
  • 1
  • 1
CommonKnowledge
  • 769
  • 1
  • 10
  • 35

2 Answers2

1

To access it as an input stream from assets:

in = new BufferedReader(new InputStreamReader(activity.getAssets().open(yourfile.pdf)));
Barak
  • 16,318
  • 9
  • 52
  • 84
1

After many many hours and quite a few cigarette breaks here is the solution. Once the readToByteBuffer has returned a ByteBuffer it is as easy as creating a new PDFFile that takes in a ByteBuffer.

Enjoy...

ShowPDF button Listener...

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

In onCreate() PdfViewerActivity...

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

edited readToByteBuffer() from here

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;
  }
CommonKnowledge
  • 769
  • 1
  • 10
  • 35
  • #CommonKnowledge which pdfreader u are using? what are you doing in openFile2(readToByteBuffer(this.getAssets().open("test.pdf")), null); – skygeek Oct 18 '12 at 07:06
  • It looks like he is using apv, or Android PDF Viewer: https://code.google.com/p/apv/ However openFile2 seems to be his own method. Im guessing it takes a ByteBuffer and converts it into a PDFFile object somehow... – Jason Ridge May 29 '14 at 13:03
  • I mean its easy to convert a bytebuffer into a PDFFile, but I dont know what he does with that – Jason Ridge May 29 '14 at 14:29