在App中显示PDF文件 [英] Show PDF file in App

查看:494
本文介绍了在App中显示PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  1. 使用以下方法打开WebView:

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

  2. 使用extern应用程序打开pdf文件:

    Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(outFile),"application/pdf"); intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); startActivity(intent);

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

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

解决方案

Android现在提供了PDF API,通过它可以轻松地在应用程序内部呈现pdf内容.

您可以在此处

下面是要从资产文件夹中的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);
    }
}

更新:此摘录来自Google开发人员提供的示例.

I found this two possibilities to show a pdf file.

  1. Open a webView with:

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

  2. Open the pdf File with a extern App:

    Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(outFile),"application/pdf"); intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); startActivity(intent);

Both of them work. But my problem is that the pdf is for internal use only and in both examples the user could download it or save it in another folder.

I know frameworks for this in iOS dev, I looking for a solution that works with Android.

解决方案

Android provides PDF API now with which it is easy to present pdf content inside application.

you can find details here

Below is the sample snippet to render from a pdf file in assets folder.

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

update: This snippet is from google developers provided samples.

这篇关于在App中显示PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆