Android intent exclude a particular file extension? - android

I want my Activity to be able to open files without any extensions and files with these two extensions: .ext1 and .ext2.
Examples:
Open: my_file.ext1, my_file.ext2, some_other_file, another_file
Ignore: my_file.pdf, myfile.mp3, etc.
<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:host="*" />
<data android:pathPattern=".*\\.ext1" />
<data android:pathPattern=".*\\.ext2" />
</intent-filter>
How to include the files without any extension and ignore the files with an extension ?
The only solution I found so far to include files without any extension is to also add:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.EDIT" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:mimeType="text/plain"
android:scheme="file"
android:host="*" />
</intent-filter>
But this also associates my Activity with .txt files. Any suggestions ?

The only way to have your app to open files with NO extension, is to use android:mimeType="*/*". Unfortunately, that will make your app open all files on the device too.
For the case of opening SPECIFIC file extensions with your app, here what you need:
<!-- Handle opening attachments with file explorer -->
<intent-filter android:priority="100">
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:host="*" android:mimeType="application/json"
android:pathPattern=".*\\.ext1"
android:scheme="file"/>
</intent-filter>
<intent-filter android:priority="100">
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:host="*"
android:mimeType="*/*"
android:pathPattern=".*\\.ext1"
android:scheme="content"/>
</intent-filter>
<intent-filter android:priority="100">
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:host="*" android:mimeType="application/json"
android:pathPattern=".*\\.ext2"
android:scheme="file"/>
</intent-filter>
<intent-filter android:priority="100">
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:host="*"
android:mimeType="*/*"
android:pathPattern=".*\\.ext2"
android:scheme="content"/>
</intent-filter>
<!-- Handle opening attachments with Downloads app -->
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<action android:name="android.intent.action.GET_CONTENT"/>
<action android:name="android.intent.action.OPEN_DOCUMENT"/>
<action android:name="android.intent.action.PICK"/>
<category android:name="android.intent.category.OPENABLE"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:mimeType="text/plain" android:pathPattern=".*\.ext1"
android:scheme="content"/>
<data android:mimeType="application/json" android:pathPattern=".*\.ext1"
android:scheme="content"/>
<data android:mimeType="application/octet-stream" android:pathPattern=".*\.ext1"
android:scheme="content"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<action android:name="android.intent.action.GET_CONTENT"/>
<action android:name="android.intent.action.OPEN_DOCUMENT"/>
<action android:name="android.intent.action.PICK"/>
<category android:name="android.intent.category.OPENABLE"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:mimeType="text/plain" android:pathPattern=".*\.ext2"
android:scheme="file"/>
<data android:mimeType="application/json" android:pathPattern=".*\.ext2"
android:scheme="file"/>
<data android:mimeType="application/octet-stream" android:pathPattern=".*\.ext2"
android:scheme="file"/>
</intent-filter>

Related

android intent filter for pick file not working on Huawei P8 and LG F70 phones

I want to open *.gpx files with my app. After some resource I placed these intent filters to my manifest file:
<!-- Intent-filter for Intents that contain the file suffix. -->
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.BROWSABLE"/>
<category android:name="android.intent.category.DEFAULT"/>
<!-- For a path to be meaningful, both a scheme and an authority must be specified. -->
<data
android:host="*"
android:mimeType="*/*"
android:pathPattern=".*\\.gpx"
android:scheme="file"/>
</intent-filter>
<!-- Intent-filter for Intents that contain a MIME type -->
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.BROWSABLE"/>
<category android:name="android.intent.category.DEFAULT"/>
<!-- This is the original mimeType which was used when creating the file. -->
<data android:mimeType="application/gpx+xml"/>
<!-- Some apps (e.g. some versions of Gmail) use the file suffix as the mimeType! -->
<data android:mimeType="application/gpx"/>
</intent-filter>
<!-- Gmail sometimes uses some strange mimeTypes when opening attachments -->
<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="content"
android:host="gmail-ls"
android:mimeType="application/octet-stream"/>
</intent-filter>
and it's works with the most of android versions and phones.
BUT as far as I know on the LG F70 with android version 4.4.2 and on the Huawei P8 Lite with android version 5.0.1 using the default file browsers it's not working. See the pictures:
Huawei P8 Lite:
LG F70:
Does anyone experienced this before? Do you have any idea which intent filter would I need?
Any answer would be really apprechiated!
Your second <intent-filter> should be meaningless on all devices, as you cannot have android:host without android:scheme.
Either:
Remove the android:host attribute, or
Move that particular <data> element into its own <intent-filter>, with a copy of the relevant actions/categories, and with an android:scheme of content
The difference between the two is whether you want to try handling application/octet-stream from any source (first bullet) or only from Gmail's content provider (second bullet).
You are on the right track there with your initial code. Here is how you can make it work:
<!-- Handle opening attachments with file explorer -->
<intent-filter android:priority="100">
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:host="*" android:mimeType="application/xml"
android:pathPattern=".*\\.gpx"
android:scheme="file"/>
</intent-filter>
<intent-filter android:priority="100">
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:host="*"
android:mimeType="*/*"
android:pathPattern=".*\\.gpx"
android:scheme="content"/>
</intent-filter>
<!-- Handle opening attachments with Downloads app -->
<intent-filter android:priority="100">
<action android:name="android.intent.action.VIEW"/>
<action android:name="android.intent.action.GET_CONTENT"/>
<action android:name="android.intent.action.OPEN_DOCUMENT"/>
<action android:name="android.intent.action.PICK"/>
<category android:name="android.intent.category.OPENABLE"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:mimeType="application/xml" android:pathPattern=".*\.gpx"
android:scheme="content"/>
<data android:mimeType="application/octet-stream" android:pathPattern=".*\.gpx"
android:scheme="content"/>
</intent-filter>
<intent-filter android:priority="100">
<action android:name="android.intent.action.VIEW"/>
<action android:name="android.intent.action.GET_CONTENT"/>
<action android:name="android.intent.action.OPEN_DOCUMENT"/>
<action android:name="android.intent.action.PICK"/>
<category android:name="android.intent.category.OPENABLE"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:mimeType="application/xml" android:pathPattern=".*\.gpx"
android:scheme="file"/>
<data android:mimeType="application/octet-stream" android:pathPattern=".*\.gpx"
android:scheme="file"/>
</intent-filter>
<!-- Handle opening attachments with Gmail -->
<intent-filter android:priority="100">
<action android:name="android.intent.action.VIEW"/>
<action android:name="android.intent.action.GET_CONTENT"/>
<data android:host="gmail-ls" android:mimeType="application/octet-stream"
android:scheme="file"/>
<category android:name="android.intent.category.BROWSABLE"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
<intent-filter android:priority="100">
<action android:name="android.intent.action.VIEW"/>
<action android:name="android.intent.action.GET_CONTENT"/>
<data android:host="gmail-ls" android:mimeType="application/octet-stream"
android:scheme="content"/>
<category android:name="android.intent.category.BROWSABLE"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
<intent-filter android:priority="100">
<action android:name="android.intent.action.VIEW"/>
<action android:name="android.intent.action.GET_CONTENT"/>
<data android:host="gmail-ls" android:mimeType="application/xml"
android:scheme="file"/>
<category android:name="android.intent.category.BROWSABLE"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
<intent-filter android:priority="100">
<action android:name="android.intent.action.VIEW"/>
<action android:name="android.intent.action.GET_CONTENT"/>
<data android:host="gmail-ls" android:mimeType="application/xml"
android:scheme="content"/>
<category android:name="android.intent.category.BROWSABLE"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
The key here is to handle proper MIME types and categories, and presume various ways of opening files on devices (i.e. file browsers, downloads app, gmail, etc.).

How can I list my app for download files?

I want to download files from browser through my application. I am trying to list my app in complete action using dialog. It shows for other action like view files, etc but in case of download files it doesn't show in dialog. how can i list my app like in the image?
I have used these filters in my activity
<intent-filter> <action android:name="android.intent.action.SEND" /> <action android:name="android.intent.action.VIEW"/> <category android:name="android.intent.category.OPENABLE"/> <category android:name="android.intent.category.BROWSABLE"/> <category android:name="android.intent.category.DEFAULT"/> <data android:mimeType="*/*"/>
I have done this by adding these filters and now it's working for all cases.
<intent-filter>
<action
android:name="android.intent.action.MAIN"/>
<category
android:name="android.intent.category.DEFAULT"/>
<category
android:name="android.intent.category.LAUNCHER"/>
<category
android:name="android.intent.category.BROWSABLE"/>
<category
android:name="android.intent.category.APP_BROWSER"/>
<category
android:name="android.intent.category.NOTIFICATION_PREFERENCES"/>
</intent-filter>
<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="googlechrome"/>
<data
android:scheme="http"/>
<data
android:scheme="https"/>
<data
android:scheme="about"/>
<data
android:scheme="javascript"/>
</intent-filter>
<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="googlechrome"/>
<data
android:scheme="http"/>
<data
android:scheme="https"/>
<data
android:scheme="about"/>
<data
android:scheme="content"/>
<data
android:scheme="javascript"/>
<data
android:mimeType="text/html"/>
<data
android:mimeType="text/plain"/>
<data
android:mimeType="application/xhtml+xml"/>
</intent-filter>
<intent-filter>
<action
android:name="android.intent.action.VIEW"/>
<category
android:name="android.intent.category.DEFAULT"/>
<data
android:mimeType="multipart/related"
android:scheme="file"/>
</intent-filter>
<intent-filter>
<action
android:name="android.intent.action.MEDIA_SEARCH"/>
<category
android:name="android.intent.category.DEFAULT"/>
</intent-filter>
<intent-filter>
<action
android:name="android.speech.action.VOICE_SEARCH_RESULTS"/>
<category
android:name="android.intent.category.DEFAULT"/>
</intent-filter>
<intent-filter
android:priority="-101">
<action
android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category
android:name="android.intent.category.DEFAULT"/>
<data
android:scheme="http"/>
<data
android:scheme="https"/>
</intent-filter>
<intent-filter>
<action
android:name="android.intent.action.SEARCH"/>
</intent-filter>
<intent-filter>
<action
android:name="com.sec.android.airview.HOVER"/>
</intent-filter>
add these two tags inside your <intent-filter> tag
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
add this code in your manifest inside your launcher activity tag.
<activity
android:name=".YourActivity"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</activity>
Try downloading the files using the API http://developer.android.com/reference/android/app/DownloadManager.html

How can I specify an intent filter to recognize jpg files, but not jpeg in Android?

Basically, I want my app to be able to open JPG or jpg files, but not files with the file extension JPEG or jpeg. This is what I have for intent filters, but it is also accepting JPEG and jpeg:
<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="content" android:mimeType="application/octet-stream" android:pathPattern=".*\\.jpg" />
<data android:scheme="file" android:mimeType="application/octet-stream" android:pathPattern=".*\\.jpg" />
</intent-filter>
<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" android:pathPattern=".*\\.jpg" />
</intent-filter>
<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="content" android:mimeType="application/octet-stream" android:pathPattern=".*\\.JPG" />
<data android:scheme="file" android:mimeType="application/octet-stream" android:pathPattern=".*\\.JPG" />
</intent-filter>
<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" android:pathPattern=".*\\.JPG" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="image/jpg" android:pathPattern=".*\\.jpg"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="image/jpg" android:pathPattern=".*\\.JPG"/>
</intent-filter>
How can I improve this intent filter to handle only specific image file extensions?
You're capturing JPEG images because you specified the android:mimeType of image/jpg, which JPEG images fit. Remove that tag from your <data> element and just specify android:pathPattern.

Open app for specific filetype (from email attachment)

As per my study to support a filetype applying filter to an Activity should be enough,
<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="https" android:host="*" android:pathPattern=".*\\.filetype"/>
<data android:scheme="http" android:host="*" android:pathPattern=".*\\.filetype"/>
<data android:scheme="file" android:host="*" android:pathPattern=".*\\.filetype"/>
<data android:scheme="content" android:host="*" android:pathPattern=".*\\.filetype"/>
</intent-filter>
But it does not work for email attachments. When try to open the attachments it only show a message, that no support app found for this filetype.
This snipped code works for email attachments,
<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:pathPattern=".*\\.fileType" />
<data android:host="*" />
<data android:mimeType="application/*" />
</intent-filter>
But for this filter, my application appears for any kind of filetype.
So my question is,
1.What is the proper way to open an email attachment with my app? So that myapp try to open only for the specific filetype.
You need a launcher default to launch based on type.
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.gosylvester.bestrides.ImageTextListViewActivity" />
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/vnd.google-earth.kml+xml" />
</intent-filter>
</activity>

ActivityNotFoundException - Intent Filter doesn't work anymore

I have an application that wants to handle SGF files. It has these filters in the manifest:
<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="http"/>
<data android:host="*"/>
<data android:pathPattern=".*\\.sgf"/>
</intent-filter>
<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="http"/>
<data android:mimeType="application/x-go-sgf"/>
</intent-filter>
<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="*/*" />
<data android:host="*" />
<data android:pathPattern=".*\\.sgf"/>
</intent-filter>
The idea is to handle SGF files opened in the browser or from a file. This was working until recently. I'm not sure exactly when it stopped working, but it wasn't because of a code change. I have another application that is trying to start the activity and it's getting an exception:
No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///mnt/sdcard/gogrinder/Mastering%20the%20Basics/Vol.%202%20-%20One%20Thousand%20and%20One%20Life-and-Death%20Problems/6%20-%20Five-move%20problems%20-%20Black%20to%20kill/prob144.sgf }
Shouldn't this intent match one of the filters above?
Opening an SGF file from the browser or file explorer does cause my activity to start, but doing it programmatically as above doesn't work anymore.
I also have an app which opens SGF files, and it works fine. Compared to your code, I added additional mimeType and host attributes :
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:host="*" />
<data android:scheme="file" />
<data android:pathPattern=".*\\.sgf" />
<data android:mimeType="*/*" />
</intent-filter>
<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:host="*" />
<data android:scheme="http" />
<data android:pathPattern=".*\\.sgf" />
<data android:mimeType="*/*" />
</intent-filter>
<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:host="*" />
<data android:scheme="http" />
<data android:mimeType="application/x-go-sgf" />
</intent-filter>
If it still doesn't work, it may be a problem with the file path. The pathPattern doesn't work very well when the path includes several dots. Check this question :
pathPattern to match file extension does not work if a period exists elsewhere in the file name?

Categories

Resources