Android - Detect URL mime type?

后端 未结 3 964
时光说笑
时光说笑 2021-01-18 03:46

In my Android app, I have various URLs that I access from a database and then open a WebView to display that URL. Typically the url looks something like this:



        
3条回答
  •  我在风中等你
    2021-01-18 04:13

    You can find out what mime type of content in such way:

    webView.setDownloadListener(new DownloadListener() {
        @Override
        public void onDownloadStart(String url, String userAgent,
                String contentDisposition, String mimetype,
                long contentLength) {
    
            //here you getting the String mimetype
            //and you can do with it whatever you want
        }
    });
    

    In this method you can check if mimetype is pdf and show it through Google Docs in WebView using modified url like this:

    String pdfPrefixUrl = "https://docs.google.com/gview?embedded=true&url="
    
    if ("application/pdf".equals(mimetype)) {
         String newUrl = pdfPrefixUrl + url;
         webView.loadUrl(newUrl);
    }    
    

    Hope it will help!

提交回复
热议问题