4

I need to show a local pdf in my android app, this pdf needs to be included in my app package, I have this but I dont know how to build a File class.

public void loadreglamento(View v){
        //Im supposed to give a path with the file but I really dont know how
        Uri path = Uri.fromFile(file);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(path, "application/pdf");
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);

    }

This code is Ok for me, I dont care If the user see the document in an external viewer/editor.

I also need to know where I have to copy the PDF file.

Thanks in advance.

Karlo A. López
  • 2,548
  • 3
  • 29
  • 56

1 Answers1

0

First to answer to your question: "How to build a File class?"

Below is a simple example.

File myFile = new File("Your any type of file url");

You should also read the Android Developer Reference on File class, which provides information on various constructors to create a File.

But to create a File, you'll need to provide the URL of the PDF file. Since you want to include the PDF file into your app package, you cannot access it by using a static URL.

There are two methods to include your files in app package:

  1. Resources: By including files in your Resources, you can access them by using their ID.

  2. Assets: By including files in your Assets, you can access them by using Asset Manager.

In your case, I would recommend using the Assets method.

You cannot open PDF files directly from above methods, unless your code has PDF reading functionality.

So to solve your problem, you first need to copy your PDF file from Assets to some location on SD-Card or Internal Storage and then you can access the file. Here is a solution to read the PDF file by copying it first by Sunil at Stack Overflow:

Read a pdf file from assets folder

I hope this helps. Happy Coding.

Community
  • 1
  • 1
Raj Kumar
  • 111
  • 2