android webview pdf viewing

我的未来我决定 提交于 2020-01-12 10:10:29

问题


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

}

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?


回答1:


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



回答2:


I think this is the simplest code. Try this:

private class HelpClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        if (url.endsWith(".pdf")){
            // Load "url" in google docs
            String googleDocs = "https://docs.google.com/viewer?url=";
            view.loadUrl(googleDocs + url);

        }
        else {
            // Load all other urls normally.
            view.loadUrl(url);
        }

//            view.loadUrl(url);
        frameLayout.setVisibility(View.VISIBLE);
        return true;
    }
}



回答3:


Get the code from below link. You can open pdf file at runtime. Means you click on button and it should open pdf file,then get the code here.

https://github.com/Talha609/Android-WebView-PDF-ProgressBar



来源:https://stackoverflow.com/questions/9455425/android-webview-pdf-viewing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!