pathPattern to match file extension does not work if a period exists elsewhere in the file name?

后端 未结 3 537
挽巷
挽巷 2020-11-27 03:06

I see numerous examples of using pathPattern to define an intent-filter for a specific file extension/type; for example, pathPattern=\".*\\\\.xyz\".

Unf

相关标签:
3条回答
  • 2020-11-27 03:41

    Ran into the same problem trying to open a file in a dot folder. I found I could just add multiple "data" elements, depending on how many dots I expected to have in my paths:

    <data android:pathPattern=".*\\.mytype"/>
    <data android:pathPattern=".*\\..*\\.mytype"/>
    <data android:pathPattern=".*\\..*\\..*\\.mytype"/>
    <data android:pathPattern=".*\\..*\\..*\\..*\\.mytype"/>
    

    Ugly, though. Anyone know a better way?

    0 讨论(0)
  • 2020-11-27 03:48

    The android team chose an unfortunate way to implement pathPattern. You can take a look at how the pattern matching is implemented in the android.os.PatternMatch class:

    https://github.com/android/platform_frameworks_base/blob/master/core/java/android/os/PatternMatcher.java

    We're used to .* working like it does in a regular expression, where the * match is greedy and will match as many characters as possible. In PatterMatch's implementation, the match is not greedy. The .* will match as many characters as it can, until it finds a match for the next character in the string.

    Example:

    String: "/mnt/my.file.mytype"
    pathPattern: ".*\\.mytype"

    The ".*" in the pathPattern will match the substring "/mnt/my", and hence will fail to match the string.

    Given this limitation, I don't see a way to write a pathPattern that can match any string that ends in ".mytype". The best you can do is follow Jason's solution to add additional patterns to match paths with as many dots as you are willing to specify patterns.

    0 讨论(0)
  • 2020-11-27 03:49

    I used this:

    <activity
       android:name=".activity.GifActivity"
       android:label="Cool Player ^_^">
       <intent-filter>
           <action android:name="android.intent.action.VIEW" />
           <category android:name="android.intent.category.DEFAULT" />
           <data android:scheme="http"/>
           <data android:scheme="https" />
           <data android:host="*"/>
           <data android:pathPattern="/.*\\.mp4"/>
       </intent-filter>
    </activity>
    

    This will open up following url:

    • http://i.imgur.com/6162727/2727727/17177/AXTI1Gr.mp4
    • http://i.imgur.asdf.lalala.com/6162727/2727727/17177/AXTI1Gr.mp4
    • http://i.imgur.com/AXTI1Gr.mp4

    Will not open up for the following:

    • http://arstechnica.co.uk/gadgets/2015/10/android-6-0s-auto-backup-for-apps-perfect-data-backup-for-the-1-5
    • http://i.imgur.asdf.lalala.com/6162727/2727727/17177/AXTI1Gr.mp3
    • http://i.imgur.com/FqMAE9H.gifv

    I think the key here is host="*". I haven't done thorough study on this. But it works for me, hope it helps someone out there too.

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