Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct Android intent-filter configuration to associate a file type with an Activity?

This question has been asked [numerous times] before, but I have not seen any definitive answers, or examples of code that actually works.

I would like to associate an Activity with a particular file type.

For discussion, assume that I want my Activity to be associated with PDFs.

Here is what I currently have. I have experimented with many different values and combinations of values in the intent-filter, but I have yet to get my Activity to start when a PDF is selected.

<activity name="com.mycompany.MyActivity">
    <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:scheme="file" />
        <data android:mimeType="application/pdf" />
        <data android:pathPattern="\\*\\.pdf" />
        <data android:host="*" />
    </intent-filter>
</activity>

Does anyone know how to actually make this work?

like image 274
dazhu Avatar asked Dec 28 '22 05:12

dazhu


2 Answers

Have you tried with that simple version :

<activity name="com.mycompany.MyActivity">
    <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="application/pdf" />
    </intent-filter>
</activity>
like image 173
Kevin Gaudin Avatar answered Dec 30 '22 19:12

Kevin Gaudin


Your pathPattern is definitively wrong and you are restricting it too much with the mimetype.

Try the following:

<activity name="com.mycompany.MyActivity">
  <intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.BROWSABLE" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:scheme="http" />
    <data android:host="*" />
    <data android:pathPattern=".*\\.pdf" />
  </intent-filter>
  <intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.BROWSABLE" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:scheme="http" />
    <data android:host="*" />
    <data android:mimeType="application/pdf" />
  </intent-filter>
  <intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:scheme="file" />
    <data android:host="*" />
    <data android:pathPattern=".*\\.pdf" />
  </intent-filter>
</activity>
like image 37
Phyrum Tea Avatar answered Dec 30 '22 19:12

Phyrum Tea