Android - 检测 URL mime 类型?

发布于 2024-12-08 08:46:53 字数 440 浏览 1 评论 0原文

在我的 Android 应用程序中,我有从数据库访问的各种 URL,然后打开 WebView 来显示该 URL。通常,该 url 看起来像这样:

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

这些引用链接总是重定向/转发到另一个 url。有时,生成的 url 直接指向图像。有时是 PDF。有时只是另一个 HTML 页面。

无论如何,我需要能够区分这些不同类型的页面。例如,如果生成的 URL 链接到 PDF 文件,我想使用 Google Docs Viewer 技巧来显示它。如果它只是一个普通的 HTML 页面,我想简单地显示它,如果它是一个图像,我计划下载该图像并以某种方式在我的应用程序中显示它。

我认为解决此问题的最佳方法是确定生成的 url 的 mime 类型。你如何做到这一点?还有更好的方法来实现我想要的吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

长不大的小祸害 2024-12-15 08:46:53

您可以通过以下方式找出内容的 mime 类型:

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

在这种方法中,您可以检查 mimetype 是否为 pdf,并使用修改后的 url 通过 Google Docs 在 WebView 中显示它,如下所示:

String pdfPrefixUrl = "https://docs.google.com/gview?embedded=true&url="

if ("application/pdf".equals(mimetype)) {
     String newUrl = pdfPrefixUrl + url;
     webView.loadUrl(newUrl);
}    

希望它会有所帮助!

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!

我不在是我 2024-12-15 08:46:53

这是我获取 mime 类型的解决方案。

它还可以在主线程 (UI) 上运行,并提供 B 计划来猜测 MIME 类型(但不是 100% 确定)

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

注释:

  • 我添加了 150 毫秒超时:请随意调整它,或者如果您从主线程外部调用它,请将其删除(并且您可以等待 URLCconnection 完成它的工作)。另外,如果您在主线程之外使用 ThreadPolicy,那么 ThreadPolicy 的东西就没用了。关于这一点...

  • 对于那些想知道为什么我在主线程上允许网络的人,原因如下:

    我必须找到一种从主线程获取 mime 类型的方法,因为 WebViewClient。 shouldOverrideKeyEvent (WebView view, KeyEvent event) 在主线程中调用,我的实现需要知道 mime 类型才能返回适当的值(true 或 false)

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)

不喜欢何必死缠烂打 2024-12-15 08:46:53

我认为内容类型http标头应该可以解决问题:

内容类型

I think content type http header should do the trick:

Content type

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文