【问题标题】:PDF view from String value来自字符串值的 PDF 视图
【发布时间】:2013-01-12 14:54:27
【问题描述】:

我的要求是,我需要为我的应用以 pdf 格式显示内容。我正在通过 HTTP 调用获取文件的 InputStream。如何将此 InputStream 转换为 PDF 视图而不存储在 SD 卡中?

我可以使用下面的代码:

        Intent intent = new Intent(Intent.ACTION_VIEW);
     intent.setDataAndType(Uri.fromFile(new File(File Path)), "application/pdf");
     intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      startActivity(intent);

但是有没有其他方式可以在 PDF 中显示?

【问题讨论】:

  • 你可以试试 InputStream object = this.getResources() .openRawResource(R.raw.fileName);
  • 上一条评论与问题无关。

标签: android


【解决方案1】:

还有另一种在 Android 应用程序中显示 PDF 的方法,即使用 http://docs.google.com/viewer 的支持将 PDF 文档嵌入到 android webview

String doc="<iframe src='http://docs.google.com/viewer?url=+location to your PDF File+' 
              width='100%' height='100%' 
              style='border: none;'></iframe>";

示例如下所示

 String doc="<iframe src='http://docs.google.com/viewer?url=http://www.iasted.org/conferences/formatting/presentations-tips.ppt&embedded=true' 
              width='100%' height='100%' 
              style='border: none;'></iframe>";

代码

    WebView  wv = (WebView)findViewById(R.id.webView); 
    wv.getSettings().setJavaScriptEnabled(true);
    wv.getSettings().setPluginsEnabled(true);
    wv.getSettings().setAllowFileAccess(true);
    wv.loadUrl(doc);
    //wv.loadData( doc, "text/html",  "UTF-8");

并在清单中提供

<uses-permission android:name="android.permission.INTERNET"/>

this

注意:我不知道各种安卓版本的兼容性问题

【讨论】:

    【解决方案2】:

    试试这个方法:

       File m_file=new File("/sdcard/myPdf.pdf");
       Uri m_uri=Uri.fromFile(m_file);
    
    try
      {
        Intent intentUrl = new Intent(Intent.ACTION_VIEW);
         intentUrl.setDataAndType(m_uri, "application/pdf");
         intentUrl.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
         startActivity(intentUrl);
       }
    catch (ActivityNotFoundException e)
    {
        Toast.makeText(m_context, "No PDF Viewer Installed", Toast.LENGTH_LONG).show();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-26
      • 1970-01-01
      相关资源
      最近更新 更多