associate file type with android app fails

2019-05-02 00:16发布

问题:

I'm struggling to associate my app with a particular file type (.stl). A recommendation here from Kevin doesn't have any effect. When trying to launch a file from a file manager, my app isn't one of the options suggested.

His code is:

<activity name="com.mycompany.MyActivity">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:mimeType="application/pdf" />
    </intent-filter>
</activity>

(Except I have stl not pdf). I've tried all the variations on this theme I can find.

The system seems unaware that my app is available to open this kind of file. Am I doing something stupid?

Many thanks for any suggestions.

EDIT:

I have the answer. Basically you have to state that you are allowing any old mimetype and relying on the file extension. Using the 'correct' mimetype (application/sla) didn't work. This works:

<data android:scheme="file" 
      android:mimeType="*/*" 
      android:pathPattern=".*\\.stl" />

Now when I select a file in Astro my app is suggested as one which will open this kind of file.

回答1:

I'm struggling to associate my app with a particular file type (.stl).

Generally, Android does not use file extensions to indicate type. Android uses MIME types to indicate type.

When trying to launch a file from a file manager, my app isn't one of the options suggested.

Well, the only guaranteed-solid answer is:

Step #1: Determine the MIME type that .stl files are associated with

Step #2: Use that MIME type in your <activity> element for the android:mimeType attribute

Step #3: Force each and every file manager author to implement support for your MIME type for when they encounter .stl files

Step #3, of course, is the problem.

Android, like iOS and other operating systems, are trying desperately to hide the concept of "files" from the user. That is why there is no file manager built into Android, why few devices bundle one, and why there is little support for the concept in the OS.

You are welcome to try to use <data android:scheme="file" android:pathPattern=".*\\.stl" /> in your <data> element to support things based on file extension, though I'm not sure if that works well. You might also need android:host="*" in that element -- Android itself does not use pathPattern for the file scheme, and the third-party code I am examining shows the element both ways. Again, I have no idea if it works.