简体   繁体   English

如何在android中阅读pdf

[英]How to read a pdf in android

I want to read a PDF file in android. 我想在android中读取PDF文件。 I placed my PDF files in the assets folder. 我将PDF文件放在assets文件夹中。

How can i read the PDF file from there? 我怎么从那里读取PDF文件?

PDF Reader Link PDF阅读器链接

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. 我还想在WebView中打开一个PDF文件。 So is it possible to read PDF in WebView? 那么可以在WebView中阅读PDF吗?

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

You need to have a application which can support that mimetype and open it. 您需要一个可以支持该mimetype并打开它的应用程序。 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 . 使用Adobe Reader等外部安装的应用程序读取pdf的另一个很好的解决方案。

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. 谷歌缓存显示格式,如pdf,ppt,docx等。您根本不需要安装任何应用程序。 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();
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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