首页 文章

在App中显示PDF文件

提问于
浏览
3

我发现这两种可能性来显示pdf文件 .

  • 使用以下命令打开webView:

webView.loadUrl("https://docs.google.com/gview?embedded=true&url="+uri);

  • 使用extern App打开pdf文件:

Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(不过outFile), “应用/ PDF”); intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); startActivity(意向);

他们都工作 . 但我的问题是pdf仅供内部使用,在这两个示例中,用户可以下载或将其保存在另一个文件夹中 .

我知道iOS开发中的框架,我正在寻找适用于Android的解决方案 .

3 回答

  • 1

    Android现在提供PDF API,很容易在应用程序中呈现pdf内容 .

    你可以找到详细信息here

    下面是要从assets文件夹中的pdf文件呈现的示例代码段 .

    private void openRenderer(Context context) throws IOException {
        // In this sample, we read a PDF from the assets directory.
        File file = new File(context.getCacheDir(), FILENAME);
        if (!file.exists()) {
            // Since PdfRenderer cannot handle the compressed asset file directly, we copy it into
            // the cache directory.
            InputStream asset = context.getAssets().open(FILENAME);
            FileOutputStream output = new FileOutputStream(file);
            final byte[] buffer = new byte[1024];
            int size;
            while ((size = asset.read(buffer)) != -1) {
                output.write(buffer, 0, size);
            }
            asset.close();
            output.close();
        }
        mFileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
        // This is the PdfRenderer we use to render the PDF.
        if (mFileDescriptor != null) {
            mPdfRenderer = new PdfRenderer(mFileDescriptor);
        }
    }
    

    更新:此片段是来自谷歌开发者提供的样本 .

  • 4

    许多库可用于在您自己的应用程序中显示pdf .

    有关使用 android-pdfView 的工作示例,请参阅blog post . 它演示了库的基本用法,通过垂直和水平滑动将pdf显示在视图上 .

    pdfView = (PDFView) findViewById(R.id.pdfView);
    pdfView.fromFile(new File("/storage/sdcard0/Download/pdf.pdf")).defaultPage(1).enableSwipe(true).onPageChange(this).load();
    
  • 3

    您可以在自己的查看器中显示PDF

    Tier是你应该看的几个开源pdf查看器:http://androiddeveloperspot.blogspot.com/2013/05/android-pdf-reader-open-source-code.html

    您可以加密pdf并确保您的查看器只能解密它 .

相关问题