0

What's the best way to make my android app be able to handle all type of files and all file extensions? For example selecting a pdf file or an image file and choose "open with" and be able to select my app.I know how to do it if I specify the file extensions but how to make it general?

Thanks

  • 1
    [Here is a list](https://www.sitepoint.com/mime-types-complete-list/) of almost all MIME types. See [this answer](https://stackoverflow.com/a/9872529/1827254) to see how to implement that. But it's almost impossible for an app to support all of them. – Eselfar Jun 21 '18 at 15:43

1 Answers1

2

Here is how I defined your AndroidManifest.xml

<intent-filter>
    <data android:scheme="file" />
    <data android:mimeType="*/*" />
    <data android:pathPattern=".*\\.*" />
    <data android:host="*" />
</intent-filter>

The scheme of file indicates that this should happen when a local file is opened.

mimeType can be set to */* to match any mime type.

pathPattern is where you specify what extension you want to match. The .* at the beginning matches any squence of characters. These strings require double escaping, so \\. matches a literal period. Then, you end with your file extension. One caveat with pathPattern is that .* is not a greedy match like you would expect if this was a regular expression. This pattern will fail to match paths that contain a . before the .*.

Finally, according to the Android documentation, both host and scheme attributes are required for the pathPattern attribute to work, so just set that to the wildcard to match anything.

Sho_arg
  • 383
  • 3
  • 13