11

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?

Community
  • 1
  • 1
Nishant Shah
  • 3,442
  • 5
  • 25
  • 34

5 Answers5

15

u can download PDFbox API to read PDF in android try this link: http://pdfbox.apache.org/

amukhachov
  • 5,822
  • 1
  • 41
  • 60
  • 1
    As I understood PDFBox does not support the Android plattform due to references to awt and swing. – Janusz Jun 22 '12 at 13:35
8

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

Vinay
  • 4,743
  • 7
  • 33
  • 43
  • Thanks for the quick reply. But if i want to open a PDF file through WebView, how can i read that PDF file. – Nishant Shah Sep 30 '10 at 13:54
  • You cant read a PDF file through WebView. Try it yourself, open the browser and google for a pdf. Try to open it... you will be prompted to download it and open it with another application... – WarrenFaith Sep 30 '10 at 14:11
  • Yes you are right. Another alternative i found out that convert pdf pages into png and display them in webview. – Nishant Shah Sep 30 '10 at 19:20
4

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();
    }
}
  • I think Your code works fine. But will you please tell me what is that mRealPath ? I want the value of the mRealPath. – Shreyash Mahajan Nov 11 '11 at 12:40
  • @iDroid mRealPath is the path of your Pdf file and "application_type" is applications. –  Jan 10 '12 at 12:54
  • @Azharahmed: Your device should have installed the "ThinkOffice". –  Feb 20 '12 at 08:42
  • if i want to make an seprate applicatioin for a PDF reader or viewer then which lib n how them i have to use....?? – SilentKiller Feb 21 '12 at 04:19
  • Does your snippet call an external application? If yes, it is not good solution. I am finding a snippet which really reads pdf directly. – emeraldhieu Apr 26 '12 at 06:25
0

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.

0
 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();
        }
    }
Satyawan Hajare
  • 1,112
  • 11
  • 10
  • Above code is working fine by using File provider which is added in Android Manifext and using android read and write permission as well as must be added android:grantUriPermissions="true" – Satyawan Hajare Apr 03 '19 at 09:47