Android的web视图PDF浏览 [英] android webview pdf viewing

查看:147
本文介绍了Android的web视图PDF浏览的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时通讯建立一个应用程序,应该允许用户点击链接到一个PDF文档中的网页视图,并在视图中显示它。 IM使用谷歌文档查看PDF文件的工作,但我的问题是,我只希望此方法,为PDF环节的工作,而不是每一个环节,我的继承人code:

im building an app that should allow users to click a link to a pdf document in a webview and display it in the view. im using google docs viewing pdf files work, but my problem is that i only want the method to work for pdf links and not every link, heres my code:

public class HelloWebViewActivity extends Activity 
{
WebView mWebView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mWebView = (WebView) findViewById(R.id.webview);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.loadUrl("https://nuigalway.blackboard.com/webapps/login/");
    mWebView.setWebViewClient(new HelloWebViewClient());

}

private class HelloWebViewClient extends WebViewClient 
{
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url)
    {
        String googleDocs = "https://docs.google.com/viewer?url="; 
        mWebView.loadUrl(googleDocs + url);
        return true;
    }
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack())
    {
        mWebView.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

}

我认为错误在shouldOverrideUrlLoading方法。有没有可以让我打开PDF格式的URL,只有当文件本身被点击web视图的任何方法?

i think the error is in the shouldOverrideUrlLoading method. Is there any method that would allow me to open the pdf url only when the file itself is clicked in the webview?

推荐答案

shouldOverrideUrlLoading 逻辑不正确。在网​​址参数是这是越来越点击的URL。所以,你要截取任何PDF链接,而是让web视图处理所有其他URL正常。

Your shouldOverrideUrlLoading logic is incorrect. The url parameter is the url which is getting clicked. So you want to intercept any pdf links, but let the webview handle all other urls normally.

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url)
    {
        if (url.endsWith(".pdf"))
        {
            // Load "url" in google docs
        }
        else
        {
            // Load all other urls normally.
            view.loadUrl(url);
        }

        return true;
     }

这篇关于Android的web视图PDF浏览的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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