【问题标题】:Load the pdf file in app from assets从资产加载应用程序中的pdf文件
【发布时间】:2013-02-12 19:22:19
【问题描述】:

我的资产中存储了一个 PDF 文件。我想从我的资产加载 PDF 并在应用程序本身中阅读它,而不使用任何 3rd 方应用程序来查看。

我在link 中得到了解决方案。从 sdcard 中选择文件时效果很好。

【问题讨论】:

  • 您是否考虑过将文件从 assets 文件夹复制到 SD 卡?
  • 检查this!一个

标签: android pdf


【解决方案1】:

以下 sn-p 可能会帮助您从asset 文件夹访问文件然后打开它:

private void ReadFromAssets()
{
    AssetManager assetManager = getAssets();

    InputStream in = null;
    OutputStream out = null;
    File file = new File(getFilesDir(), "file.pdf");
    try
    {
        in = assetManager.open("file.pdf");
        out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);

        copyFile(in, out);
        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;
    } catch (Exception e)
    {
        Log.e("tag", e.getMessage());
    }

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(
            Uri.parse("file://" + getFilesDir() + "/file.pdf"),
            "application/pdf");

    startActivity(intent);
}

copyFile方法如下:

private void copyFile(InputStream in, OutputStream out) throws IOException
{
    byte[] buffer = new byte[1024];
    int read;
    while ((read = in.read(buffer)) != -1)
    {
        out.write(buffer, 0, read);
    }
}

编辑

为此,您必须使用外部库。在下面的链接中解释得很好: Render a PDF file using Java on Android

希望这会对你有所帮助。

【讨论】:

  • 是的,是的,我已经看到我正在使用 PDFViewer 库,但你还没有解释如何实现,请你帮我解决一下
  • GitHib 上的项目解释了这一点。如果您在提到的链接上没有找到链接,请使用 thsi 直接链接。 github.com/jblough/Android-Pdf-Viewer-Library
  • 不,我的意思是说我可以使用 PDFViewer lib 显示 sdcard 中的 pdf,但使用相同的 lib 来从 assests 中读取文件?
  • 看看我之前给你的答案...正在从资产中获取文件...
  • 在打开库部分时继续。
【解决方案2】:

如果你能用webview打开它就更好了

WebView web = (WebView) findViewById(R.id.webView1);

web.loadUrl("file:///android_asset/yourpdf.pdf");

希望它有效。

糟糕,我刚刚检查了,pdf 无法在 web 视图中加载 对不起

【讨论】:

  • 不,伙计,这是一种昂贵的方法,而且加载需要一些时间,用户无法等待
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-15
  • 1970-01-01
  • 1970-01-01
  • 2019-06-22
  • 2023-03-17
  • 2017-12-26
相关资源
最近更新 更多