Android - Detect URL mime type?

后端 未结 3 921
时光说笑
时光说笑 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:07

    Here is my solution to get mime type.

    It also works on main thread (UI) and provides a B plan to guess mime type (not 100% sure though)

    import java.net.URL;
    import java.net.URLConnection;
    
    public static String getMimeType(String url)
    {
        String mimeType = null;
    
        // this is to handle call from main thread
        StrictMode.ThreadPolicy prviousThreadPolicy = StrictMode.getThreadPolicy();
    
        // temporary allow network access main thread
        // in order to get mime type from content-type
    
        StrictMode.ThreadPolicy permitAllPolicy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(permitAllPolicy);
    
        try
        {
            URLConnection connection = new URL(url).openConnection();
            connection.setConnectTimeout(150);
            connection.setReadTimeout(150);
            mimeType = connection.getContentType();
            Log.i("", "mimeType from content-type "+ mimeType);
        }
        catch (Exception ignored)
        {
        }
        finally
        {
            // restore main thread's default network access policy
            StrictMode.setThreadPolicy(prviousThreadPolicy);
        }
    
        if(mimeType == null)
        {
            // Our B plan: guessing from from url
            try
            {
                mimeType = URLConnection.guessContentTypeFromName(url);
            }
            catch (Exception ignored)
            {
            }
            Log.i("", "mimeType guessed from url "+ mimeType);
        }
        return mimeType;
    }
    

    Notes:

    • I added a 150 ms timeout: feel free to tune that, or remove it if you call this from outside the main thread (and it's ok for you to wait for URLCconnection to do it's job). Also, the ThreadPolicy stuff is useless if you use this outside the main thread. about that...

    • For those who wonder why I allow network on main thread, here is the reason:

      I had to find a way to get mime type from main thread because WebViewClient. shouldOverrideKeyEvent (WebView view, KeyEvent event) is called in main thread and my implementation of it needs to know the mime type in order to return the appropriate value (true or false)

    0 讨论(0)
  • 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!

    0 讨论(0)
  • 2021-01-18 04:27

    I think content type http header should do the trick:

    Content type

    0 讨论(0)
提交回复
热议问题