首先要在 AndroidManifest.xml 裡面定義 App 開啟檔案的頁面

重點是要在頁面中定義 intent-filter 來過濾可開啟的檔案類型

data 定義檔案類型 scheme 可能是 file(如 android 系統內下載下載來的檔案)

或是其他 app 中的 content(如 email app 的 附檔, dropbox app 的檔案)

pathPattern 用來定義附檔名格式

action 定義 android 中使用 View action

<activity
    android:name="com.sample.ImportDataActivity"
    android:label="@string/app_name"
    android:screenOrientation="portrait" >
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data
            android:mimeType="*/*"
            android:scheme="file"
            android:pathPattern=".*\\.mytype" />
        <data
            android:mimeType="*/*"
            android:scheme="content"
            android:pathPattern=".*\\.mytype" />
    </intent-filter>
</activity>

接著就在 activity 中取得匯入資料處理啦

public class ImportDataActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_import);
        // get file to do something
        Uri data = getIntent().getData();
    }
}

 

ps.如果你的檔案名稱有多個點會使得pathPattern對應不到

目前解法就只有看有幾個點多加pathPattern給他...

<dataandroid:pathPattern=".*\\.mytype"/>

<dataandroid:pathPattern=".*\\..*\\.mytype"/>

<dataandroid:pathPattern=".*\\..*\\..*\\.mytype"/>

<dataandroid:pathPattern=".*\\..*\\..*\\..*\\.mytype"/>

A limitation in Intent-filter of Android Application 

 

Registering for file types in android

arrow
arrow
    全站熱搜

    JohnDX 發表在 痞客邦 留言(0) 人氣()