How to open a local PDF in android?

Karlo A. López

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.

Raj Kumar

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Cordova open local Pdf file on Android

From Dev

How to open pdf file inside android browser?

From Dev

how to open PDF in android using pdfview

From Dev

How to open pdf file inside android browser?

From Dev

How to open PDF file in android device?

From Dev

How to open a local PDF in a Windows 8/Cordova app?

From Dev

Open a local pdf file with cordova

From Dev

Open local PDF file in iBooks

From Dev

ionic - open local PDF file

From Dev

How to open a local java class from sdcard in android

From Dev

How to open a pdf file

From Dev

Android development: How can i store pdf file in a android app as local file and read that pdf file?

From Dev

open chrome with local file in android

From Dev

Trying to open pdf file Android

From Dev

How to open local bitcoin database

From Dev

How to open local bitcoin database

From Dev

How to open PDF files safely

From Dev

How to open a pdf in Ubuntu 20.04?

From Dev

Open local PDF file in WebView using Google Docs

From Dev

HTML - Open local PDF in a new window using a button

From Dev

Open local PDF file in WebView using Google Docs

From Dev

Is it possible to use CDVInAppBrowser to open local file (says pdf)?

From Dev

HTML - Open local PDF in a new window using a button

From Dev

Viewing a local PDF file in Android Application

From Dev

Open a pdf saved in the temp folder in android

From Dev

Open online pdf file through android intent?

From Dev

Android open pdf file via Intent

From Dev

itext pdf generator is an open source library for android?

From Dev

Android open pdf file via Intent