开发者

Android - Detect URL mime type?

开发者 https://www.devze.com 2023-04-11 12:57 出处:网络
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:

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:

http://www.mysite.com/referral.php?id=12345

These referral links always redirect/forward to another url. Sometimes the resulting url is directly to an image. Sometimes it's to a PDF. Sometimes it's to just another HTML page.

开发者_开发知识库

Anyways, I need to be able to distinguish between these different types of pages. For example, if the resulting URL links to a PDF file, I want to use the Google Docs Viewer trick to display it. If it's just a plain HTML page I want to simply display it and if it's an image, I am planning on downloading the image and displaying it in my app in a certain way.

I figure the best way to approach this is to determine the mime type of the resulting url. How do you do this? And is there a better way to accomplish what I want?


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!


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)


I think content type http header should do the trick:

Content type

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号