Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android How to create Intent Filter for custom file extension that does NOT make it part of a chooser for everything on the phone

I have seen numerous answers on here about creating an intent filter for a custom file extension, but none of them seem to be answering my question:

I have an intent filter that works right now... when I browse for my file or when I open it from an email attachment, my app will appear in the list. The file itself has a custom extension of "tgtp", but it is basically just an xml file.

The problem I am having is that although this intent filter works, it also appears to add my app to every chooser for every type of file on my phone. An example would be if I clear my contacts app defaults and click on one of my contacts, it says my app can open it.

I've tried dozens of different combinations of intent filters with different schemes, mime types, etc... and some still let me open the file if i browse with a file browser, but I specifically need to be able to open email attachments and open as file browser. I am yet to find an intent filter(s) that allow me to do that without making my app available for every other intent chooser.

Here is my current intent-filter that uses my app to open everything:

<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*" />
<data android:pathPattern=".*\\.tgtp" />   
</intent-filter>

Thank you in advance

like image 557
Nick Avatar asked Mar 01 '12 14:03

Nick


People also ask

Which components can you specify in an intent filter?

The intent filter specifies the types of intents that an activity, service, or broadcast receiver can respond. Intent filters are declared in the Android manifest file.

Can an activity have multiple intent filters?

To inform the system which implicit intents they can handle, activities, services, and broadcast receivers can have one or more intent filters.

What is Intentfilter?

An intent filter declares the capabilities of its parent component — what an activity or service can do and what types of broadcasts a receiver can handle. It opens the component to receiving intents of the advertised type, while filtering out those that are not meaningful for the component.

What is the purpose of category in an intent filter?

Categories are used for implicit Intents. So, If your Activity can be started by an implicit Intent when no other specific category is assigned to activity, activity's Intent filter should include this category. (even if you have other categories in the Intent filter).


1 Answers

The only way to solve this problem is add scheme and host attributes to your intent filter:

<intent-filter>
  <action android:name="android.intent.action.VIEW" />
  <category android:name="android.intent.category.DEFAULT" />
  <data android:scheme="file" />
  <data android:mimeType="*/*" />
  <data android:pathPattern=".*\\.tgtp" />   
  <data android:host="*" />
</intent-filter>

That is because in documentation says that android:pathPattern only works if has an scheme and host defined. http://developer.android.com/guide/topics/manifest/data-element.html

Hope it helps.

like image 104
sabadow Avatar answered Sep 27 '22 18:09

sabadow