Android Intent Filter For a Particular File Extension

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.

popup



Related Topics



Leave a reply



Submit