Intent Filter of File Extension for specific activity
There is no direct solution to this issue by using Intent Filters per Activity, So I have created FileIntentActivity which accepts following Intent Filter
<intent-filter
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
tools:ignore="AppLinkUrlError">
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.EDIT" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*" />
</intent-filter>
This FileIntentActivity is used to accept any file format and then decides further action based on file extension converted from Content URI using Cursors referred from this answer
fun getFileName(uri: Uri): String {
lateinit var cursor: Cursor
lateinit var result: String
try {
cursor = appContext.contentResolver.query(uri, null, null, null, null)
if(cursor!=null && cursor.moveToFirst()){
result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME))
}
return result
} finally {
cursor.close()
}
}
private fun getFileType(fileName: String): FileType {
return when (fileExtension) {
FileFormats.ABC_FILE_FORMAT -> FileType.ABC
FileFormats.XYZ_FILE_FORMAT -> FileType.XYZ
else -> FileType.UNKNOWN
}
}
And open the Activity1 or Activity2 based on FileType
private fun openActivityByType(fileType: FileType) {
when (fileType) {
FileType.ABC -> startActivity1()
FileType.XYZ -> startActivity2()
FileType.UNKNOWN -> showFileUnsupportedMessage()
}
}
This approach at least makes User Interaction less confusing by showing only 1 File Intent Activity instead of Multiple Activities of Single app in Android's Intent Chooser.
Android intent filter: associate app with file extension
You need multiple intent filters to address different situation you want to handle.
Example 1, handle http requests without mimetypes:
<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>
Handle with mimetypes, where the suffix is irrelevant:
<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>
Handle intent from a file browser app:
<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>
Android SEND action intent filter for a particular file extension?
I only want it to appear when the file that is being shared has a 'foo' extension (filename.foo)
Files are not shared in Android. Content is shared. Content does not have a file extension — it has a MIME type. What you want is not supported by Android.
This is the code inside the manifest
ACTION_SEND
does not use a Uri
. All of your <data>
elements are invalid, except for <data android:mimeType="*/*" />
.
I got a waring on the "pathPattern" line, saying this
You had that same warning before you added this <intent-filter>
. You will get that same warning in a brand-new Android Studio project. It is unrelated to your <intent-filter>
and simply is annoying noise from the IDE.
Why can I not get suggested file specific extension intents to work?
Based upon CommonsWare comment and the supporting link here, I decided to create a string .endsWith(".db")
check as a workaround; which involved a custom PopupDialog.class
I created for my overall application - which is a work in progress.
Related Topics
Trying to Start a Service on Boot on Android
Make an Http Request With Android
How to Dynamically Add Suggestions to Autocompletetextview With Preserving Character Status
How to Send One to One Message Using Firebase Messaging
Android Listview With Different Layouts For Each Row
How to Check If a Service Is Running on Android
How to Enable or Disable the Gps Programmatically on Android
How to Connect to a Specific Wi-Fi Network in Android Programmatically
How to Change the Status Bar Color in Android
Find Location of a Removable Sd Card
How to Copy Files from 'Assets' Folder to Sdcard
Android: Install .Apk Programmatically
How to Change Theme For Alertdialog
How to Validate an E-Mail Address
Changing API Level Android Studio
Android Imageview Zoom-In and Zoom-Out