Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8072067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:53:51+00:00 2026-06-05T13:53:51+00:00

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

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T13:53:53+00:00Added an answer on June 5, 2026 at 1:53 pm

    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;
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking for an appropriate way to store and access pdf files in
I'd like to limit access to some specific PDF files but am having a
The access to read from the db has been given to me via mssql
I need to convert PDF pages to TIF files from within my app (or
I'm trying to locate all PDF files in some folder and any subfolder, just
I'm working on restricting access of static PDF files to only logged-in users. I
My Access 2000 application is generating a PDF from a snapshot of a report
I'm using a PDF converter to access the graphical data within a PDF. Everything
Lets say I have some pdf files stored on my server and I only
My goal is to have user-uploaded files (mostly PDF) available for download only via

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.