Android intent filter for a particular file extension?

后端 未结 15 1368
我寻月下人不归
我寻月下人不归 2020-11-22 16:04

I want to be able to download a file with a particular extension from the \'net, and have it passed to my application to deal with it, but I haven\'t been able to figure out

相关标签:
15条回答
  • 2020-11-22 16:16

    On Android 4 the rules became more strict then they used to be. Use:

        <data
          android:host=""
          android:mimeType="*/*"
          android:pathPattern=".*\\.ext"
          android:scheme="file"
        ></data>
    
    0 讨论(0)
  • 2020-11-22 16:21

    I must admit that the simple task of opening attachments from emails and files from the filesystem on Android has been one of the more maddening experiences ever. It is easy to handle too many files or too few. But getting it just right is hard. Most of the solutions posted on stackoverflow didn't work correctly for me.

    My requirements were:

    • have my app handle attachments shared by my app
    • have my app handle files on filestorage that were generated by my app and have a particular extension

    Probably the best way to go about this task is to specify a custom MIME Type for your attachments. And you will probably also choose to have a custom file extension. So let's say that our app is called "Cool App" and we generate file attachments that have ".cool" at the end.

    This is the closest I got got to my goal and it works... satisfactory.

    <!-- Register to handle email attachments -->
    <!-- WARNING: Do NOT use android:host="*" for these as they will not work properly -->
    <intent-filter>
        <!-- needed for properly formatted email messages -->
        <data
            android:scheme="content"
            android:mimeType="application/vnd.coolapp"
            android:pathPattern=".*\\.cool" />
        <!-- needed for mangled email messages -->
        <data
            android:scheme="content"
            android:mimeType="application/coolapp"
            android:pathPattern=".*\\.cool" />
        <!-- needed for mangled email messages -->
        <data
            android:scheme="content"
            android:mimeType="application/octet-stream"
            android:pathPattern=".*\\.cool" />
    
        <action android:name="android.intent.action.VIEW" />
    
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
    </intent-filter>
    
    <!-- Register to handle file opening -->
    <intent-filter>
        <data android:scheme="file"
              android:mimeType="*/*"
              android:pathPattern=".*\\.cool"
              android:host="*"/>
    
        <action android:name="android.intent.action.VIEW" />
    
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
    </intent-filter>
    

    Notes:

    • The pathPattern seems to be more or less ignored for attachments (when using android:scheme="content"). If somebody gets the pathPattern to respond only to certain patterns I would be thrilled to see how.
    • The Gmail app refused to list my app in the chooser if I added the android:host="*" attribute.
    • It probably still works if these intent-filter blocks are merged but I haven't verified this.
    • To handle requests from a browser when downloading a file the android:scheme="http" can be used. Note that certain browsers might mess up the android:mimeType so experiment with android:mimeType="*/*" and check in the debugger what is actually passed through and then tighten the filtering to not end up being that annoying app that handles everything.
    • Certain File Explorers will mess up the MIME-Types for your files as well. The above intent-filter was tested with the Samsung's "My Files" app on a Galaxy S3. The FX Explorer still refuses to properly open the file and I also noticed that the app icon is not used for the files. Again, if anyone gets that to work please comment below.

    I hope you will find this useful and that you won't have to waste days going through all possible combinations. There is room for improvement so comments are welcome.

    0 讨论(0)
  • 2020-11-22 16:21

    Rather than android:path, try android:mimeType, with a value of the MIME type of this particular piece of content. Also, android:path does not accept wildcards -- use android:pathPattern for that.

    0 讨论(0)
  • 2020-11-22 16:23

    Read this https://developer.android.com/training/sharing/receive , this could help to get started. then in the manifest file try the below intent filter when registering receiver in the activity to be opened

    <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <data android:scheme="content"/>
                <category android:name="android.intent.category.BROWSABLE"/>
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="application/your_extension" />
    </intent-filter>
    

    make sure that the extension is provided without "." in it

    0 讨论(0)
  • 2020-11-22 16:30

    If you want the files to be opened directly from Gmail, dropbox or any of the buildin android file tools, then use the following code (delete 'android:host="*"' that made the file unreachable for gmail) :

    <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:pathPattern=".*\\.kdb" 
              android:mimeType="application/octet-stream"/>
    
    
    </intent-filter>
    
    <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:scheme="file" android:mimeType="*/*"     
              android:pathPattern=".*\\.kdb"/>
    </intent-filter>
    

    The data filter must be written in one statement as per Android version 4.x

    0 讨论(0)
  • 2020-11-22 16:31

    Brian's answer above got me 90% of the way there. To finish it off, for mime type I used

    android:mimeType="*/*"
    

    I suspect that previous posters have attempted to post the same detail, but the withough qoting the star slash star as code, stackoverflow diplays it as just a slash.

    0 讨论(0)
提交回复
热议问题