2012-03-08 86 views
3

我想用一個意向過濾器,這使得該應用程序打開時,點擊了fileexplorer一個zip文件Android的意圖過濾拉鍊

所以我一定要使用的MIME類型? 和什麼代碼來獲取路徑?

<activity 
    android:name=".App" 
    android:label="@string/app_name" > 
    <intent-filter> 
     <action android:name="android.intent.action.SEND" /> 

     <category android:name="android.intent.category.DEFAULT" /> 

     <data android:mimeType="text/plain" /> 

    </intent-filter> 

    </activity> 

的java代碼:

Intent intent = getIntent(); 

     // To get the action of the intent use 
     String action = intent.getAction(); 
     if (action != Intent.ACTION_SEND) { 
      throw new RuntimeException("Should not happen"); 
     } 
     // To get the data use 
     Uri data = intent.getData(); 
     URL url; 
     try { 
      url = new URL(data.getPath()); 
     } catch (MalformedURLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

回答

5

您可以使用以下IntentFilter

<intent-filter> 
    <action android:name="android.intent.action.VIEW"/> 

    <category android:name="android.intent.category.DEFAULT"/> 

    <data android:mimeType="application/zip"/> 
</intent-filter> 

當你的活動啓動時,它有一個數據URI從中可以得到的zip文件:

File zip = new File(getIntent().getData().getPath()); 
+1

mimeType是Android 5.0上的「應用程序/ x-zip-compressed」 – 2015-06-18 05:43:45

+0

@bohdan_trotsenko,它解釋了我在過去4個小時中遇到的錯誤。 _**謝謝**_ – 2017-03-14 14:32:36