代码之家  ›  专栏  ›  技术社区  ›  Nishant Shah

如何在android中读取pdf

  •  10
  • Nishant Shah  · 技术社区  · 15 年前

    我想在android中阅读PDF文件。 我把PDF文件放在assets文件夹中。

    如何从那里读取PDF文件?

    PDF Reader Link

    我已经检查了上述链接,但它不适合我。

    我还想在WebView中打开一个PDF文件。那么,在WebView中阅读PDF有可能吗?

    5 回复  |  直到 9 年前
        1
  •  7
  •   Vinay    15 年前

    你需要有一个应用程序可以支持这个mimetype并打开它。 在您的设备/模拟器中,该应用程序未安装,因此引发错误

        2
  •  15
  •   amukhachov    12 年前

    你可以下载PDFbox API在android中阅读PDF 请尝试以下链接: http://pdfbox.apache.org/

        3
  •  4
  •   user456118 user456118    12 年前

    另一个伟大的解决方案,阅读pdf使用外部安装的应用程序,如 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();
        }
    }
    
        4
  •  0
  •   user1921241 user1921241    12 年前

    在google中搜索链接并转到Cached。

        5
  •  0
  •   satyawan hajare    7 年前
     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();
            }
        }