Android自定义文件类型的Intent过滤器

huangapple go评论79阅读模式
英文:

Android Intent Filter for custom file type

问题

问题

我有一个zip文件,但带有自定义文件扩展名xyz
我希望我的应用程序只能打开具有自定义文件扩展名xyz的文件,来自:

  • Gmail,
  • Android文件浏览器中的“打开方式”,
  • Android文件浏览器中的“通过共享”。

我已经尝试过的方法

我尝试过的最佳方法就是只使用mime类型application/zip。但是这将打开带有文件扩展名**.zip**的文件。

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

我尝试添加<data android:pathPattern=".*\\.xyz"/>。但是这个问题是,只要我不指定android:schemeandroid:host,它就会被忽略 见此处

> 只有在为过滤器指定了scheme和host属性时,此属性才有意义。

一旦我像下面的代码段中所见指定了scheme和host,我甚至无法打开任何文件:

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <action android:name="android.intent.action.SEND" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:host="*" />
    <data android:scheme="*" />
    <data android:mimeType="application/zip" />
    <data android:pathPattern=".*\\.xyz"/>
</intent-filter>

可能的重复问题:

英文:

Problem

I have a zip file but with a custom file extension xyz.
I want my app to be able to open ONLY files with the custom file extension xyz from:

  • Gmail,
  • Open with from the android file explorer,
  • Share via from the android file explorer.

What I have already tried

My best shot was to just use a mime type of application/zip. However this would also open files with the file extension .zip.

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

I tried to add a <data android:pathPattern=".*\\.xyz"/>. But the problem with this is, that it will be ignored as long as I don't specify android:scheme and android:host see here.

> This attribute is meaningful only if the scheme and host attributes are also specified for the filter.

As soon as I specify the scheme and host as seen in the next code segment, I can't even open any file with my App:

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <action android:name="android.intent.action.SEND" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:host="*" />
    <data android:scheme="*" />
    <data android:mimeType="application/zip" />
    <data android:pathPattern=".*\\.xyz"/>
</intent-filter>

Possible Duplicates:

答案1

得分: 1

请使用 application/octet-stream 代替 application/zip。以下代码对我有效:

<intent-filter tools:ignore="AppLinkUrlError"
               android:icon="@mipmap/ic_launcher"
               android:label="@string/app_name"
               android:priority="50" >
  <action android:name="android.intent.action.VIEW" />
  <category android:name="android.intent.category.DEFAULT" />
  <data android:scheme="file" />
  <data android:scheme="content" />
  <data android:mimeType="application/octet-stream" />
  <data android:pathPattern=".*\.ext" />
  <data android:pathPattern=".*\..*\.ext" />
  <data android:pathPattern=".*\..*\..*\.ext" />
</intent-filter>
英文:

Use application/octet-stream instead of application/zip. Bellow works for me:

&lt;intent-filter tools:ignore=&quot;AppLinkUrlError&quot;
               android:icon=&quot;@mipmap/ic_launcher&quot;
               android:label=&quot;@string/app_name&quot;
               android:priority=&quot;50&quot; &gt;
  &lt;action android:name=&quot;android.intent.action.VIEW&quot; /&gt;
  &lt;category android:name=&quot;android.intent.category.DEFAULT&quot; /&gt;
  &lt;data android:scheme=&quot;file&quot; /&gt;
  &lt;data android:scheme=&quot;content&quot; /&gt;
  &lt;data android:mimeType=&quot;application/octet-stream&quot; /&gt;
  &lt;data android:pathPattern=&quot;.*\\.ext&quot; /&gt;
  &lt;data android:pathPattern=&quot;.*\\..*\\.ext&quot; /&gt;
  &lt;data android:pathPattern=&quot;.*\\..*\\..*\\.ext&quot; /&gt;
&lt;/intent-filter&gt;

huangapple
  • 本文由 发表于 2020年7月30日 20:07:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63172852.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定