0

我已经搜索了很多关于此的内容,但未能找到任何内容。我的目标是打开所有带有视频文件的URL(从浏览器中选择)。通常情况下,如果所有的URL与视频IE的文件扩展名结束:www.example.com/wow.mp4我可以只使用这个意图过滤我的清单:在我的应用程序中打开视频网址

<intent-filter> 
    <action android:name="android.intent.action.VIEW" /> 
    <data android:scheme="http"/> 
    <data android:scheme="https"/> 
    <data android:mimeType="video/*"> 
    <category android:name="android.intent.category.DEFAULT" /> 
    <category android:name="android.intent.category.BROWSABLE" /> 
</intent-filter> 

但处处并非如此,某些URL开始,如:

http://www.videoweed.es/mobile/....17da9f11345a424f02a5 

然后重定向到正确的链接。我想知道如何使用意图过滤器拦截这些类型的视频网址。 MXPlayer实现了此功能。

回答

1

您需要调用HTTPConnection模块来获取MIME类型,然后使用MIME类型来启动活动。 你可以参考下面的代码部分来获得mime类型的URL。

你可以参考Android - Detect URL mime type

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

谢谢,但事实并非完整的解决方案。一旦我的应用程序被选为URL,无论是从接收器还是应用程序本身,如果mimeType不是视频,那么我的代码将会运行,然后我将被打开,但无法处理意图。任何想法如何通过清单独自实现? – Aashir 2014-12-03 04:35:02

+0

我认为现在是一个框架主题,您应该在活动启动之前处理它,因此需要更改框架以支持此类功能。 – 2014-12-03 15:15:41

+0

想想这样,我希望我的应用程序处于选择器对话框中,I.E用于播放视频。所以我不能让用户启动我的应用程序只发现他选择了错误的链接,我的应用程序无法做任何事情。 – Aashir 2014-12-03 17:25:48

相关问题