Nishant Shah
Nishant Shah

Reputation: 3442

How to read a pdf in android

I want to read a PDF file in android. I placed my PDF files in the assets folder.

How can i read the PDF file from there?

PDF Reader Link

I have checked the above link but it does not work for me. It gives me an error saying that the Activity was not found.

And I also want to open a PDF file in WebView. So is it possible to read PDF in WebView?

Upvotes: 11

Views: 35815

Answers (5)

Jazz
Jazz

Reputation:

Download PDFbox API to read PDF in android.

Upvotes: 15

Satyawan Hajare
Satyawan Hajare

Reputation: 1132

 private void openPDF(final String path) {
        File file = new File(path);
        Uri uri;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            uri = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", file);
        } else {
            uri = Uri.fromFile(file);
        }

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.setDataAndType(path, "application/pdf");
        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        try {
            startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Toast.makeText(this, "Application not found", Toast.LENGTH_SHORT).show();
        }
    }

Upvotes: 0

user456118
user456118

Reputation:

Another great solution to read pdf using externally installed application like Adobe Reader.

private void openPDF(final String pathToPDF) {
    File file = new File(pathToPDF);
    Uri path = Uri.fromFile(file);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.setDataAndType(path, "application/pdf");
    try {
        startActivity(intent);
    } catch (ActivityNotFoundException e) {
        Toast.makeText(this, "Application not found", Toast.LENGTH_SHORT).show();
    }
}

Upvotes: 4

user1921241
user1921241

Reputation:

Google cached displays formats like pdf, ppt, docx, etc. And you don't need to install any application at all. Search the link in google and go to Cached.

Upvotes: 0

Vinay
Vinay

Reputation: 4783

You need to have a application which can support that mimetype and open it. In your device/emulator that app is not installed so it is throwing error

Upvotes: 8

Related Questions