0

我试图在 webview 中显示 PDF 文件。我试图通过 HTML5 加载 PDF。这是我的源代码:

WebView 网络视图;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    webview = (WebView) findViewById(R.id.webview);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.setWebViewClient(new HelloWebViewClient()); 
    webview.loadUrl("file:///android_asset/www/test.pdf");
    WebSettings settings = webview.getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setDatabaseEnabled(true);
    String databasePath = this.getApplicationContext()
            .getDir("database", Context.MODE_PRIVATE).getPath();
    settings.setDatabasePath(databasePath);
    webview.setWebChromeClient(new WebChromeClient());
}

private class HelloWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}

任何帮助将不胜感激。提前致谢。

4

3 回答 3

1

Webview does not support PDF in android.

You need to use google doc.

So you can't load PDF in webview from asset folder.

Either put your pdf in server or

Use some library to read pdf like mupdf

i have also faced this problem see this Question

or you can use Intent.ACTION_VIEW

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(filepath, "application/pdf");
startActivity(intent);
于 2012-06-02T11:29:18.590 回答
0

尝试在您的 WebView 对象中启用插件,例如

mWebView.getSettings().setPluginsEnabled(true);

Android的可能副本- 加载 PDF / PDF 查看器

于 2012-06-02T11:12:56.290 回答
-1

试试这个代码::参考这个

public class MyPdfViewActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    WebView mWebView=new WebView(MyPdfViewActivity.this);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setPluginsEnabled(true);
    mWebView.loadUrl("file:///android_asset/www/test.pdf");
    setContentView(mWebView);
  }
}

更新::

Uri path = Uri.fromFile(new File("assets/www/test.pdf"); 
Intent intent = new Intent(Intent.ACTION_VIEW); 
intent.setDataAndType(path, "application/pdf"); 
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
startActivity(intent); 
于 2012-06-02T11:13:00.460 回答