【问题标题】:Android - Read pdf and display it using IntentAndroid - 阅读 pdf 并使用 Intent 显示
【发布时间】:2014-01-30 17:45:11
【问题描述】:

我正在开发一个应用程序,我想显示资产中的 pdf 文件。我做了很多谷歌,也尝试了排列和组合的数量,但没有奏效。

代码:

private void CopyReadAssets()
{
    AssetManager assetManager = getActivity().getAssets();

    InputStream in = null;
    OutputStream out = null;
    File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/abc.pdf");
    try
    {
        in = assetManager.open("abc.pdf");
        out = getActivity().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.fromFile(file), "application/pdf");
    intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    startActivity(intent);
}

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

当我点击列表时,我调用CopyReadAssets() 函数然后它提示我您要在哪个查看器中打开然后我点击AdobeReader 然后它显示以下错误。

【问题讨论】:

    标签: android pdf android-intent


    【解决方案1】:

    我已经检查了你的代码有一些错误需要你更改,可能是你从here.复制了代码

    替换 AssetManager assetManager = getAssets();

    而不是AssetManager assetManager = getActivity().getAssets();

    直接使用File file = new File(getFilesDir(), "abc.pdf");

    代替

    File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/abc.pdf");

    希望它对你有用。!!!

    【讨论】:

    • 而且我必须使用 getActivity.getAssets().. 因为我是在 Fragment 中做的
    • 把这两个函数都放在 try catch 块中,让我知道你遇到了什么错误,
    • @Mitesh 很高兴它为您工作:) 很高兴为您提供帮助
    【解决方案2】:

    您可以通过使用网络视图来做到这一点-

         webview = (WebView) findViewById(R.id.prayertimes_webview);
         webview.getSettings().setJavaScriptEnabled(true);
         webview.getSettings().setPluginsEnabled(true);
         webview.getSettings().setSupportZoom(true);
         webview.getSettings().setBuiltInZoomControls(true);
                String urll = "http://docs.google.com/gview?embedded=true&url=" + url;
                webview.setWebViewClient(new WebViewClient()); 
                webview.loadUrl(urll);
    

    【讨论】:

    • 您可以通过将 pdf 作为 docs.google.com/gview?embedded=true&url= 发送给 google 并传递给您的 pdf 文件来打开您的 pdf。
    • @Lucky 如果 android 提供了本机功能,那么为什么&谁会尝试从远程服务器加载 pdf。(Web 视图是一个浏览器,打开 PDF 会很费力,这是一种不好的方法)
    • @chintankhetiya 如果您没有通过上述代码,您可以使用它来完成您的任务,这是替代解决方案而不是主要解决方案。
    【解决方案3】:

    无法从 PDF 阅读器查看 PDF 文件,因为您的资产中的任何内容都是您的应用程序私有的。您必须以某种方式将其公开。有几种方法。

    最复杂的方法是实现公共ContentProvider 并覆盖其中的openAssetFile 方法。通过Intent传递文件的URL,PDF阅读器应该可以使用ContentResolver并通过openAssetFileDescriptor方法获取PDF文件。

    这是一个链接。 - http://www.nowherenearithaca.com/2012/03/too-easy-using-contentprovider-to-send.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-12
      • 2011-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-07
      相关资源
      最近更新 更多