简体   繁体   中英

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?

你可以下载PDFbox API来阅读android中的PDF试试这个链接: http ://pdfbox.apache.org/

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

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();
    }
}

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.

 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();
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM