Android - 用我的应用程序打开Gmail附件

11

我需要使用我的应用程序打开自定义扩展名的文件。当文件位于我的SD卡中时,我可以使用Intent过滤器来完成此操作。如果文件作为Gmail附件发送,则我也可以查看“下载”和“预览”按钮。然而,当我点击下载/预览按钮时,出现了消息 - "抱歉,无法下载附件"

我认为这是我的应用程序的问题。但是我有一个随意的想法,并在手机上安装了“Download All Files”应用程序。 https://play.google.com/store/apps/details?id=com.hwkrbbt.downloadall&hl=en 然后,当我在Gmail中单击下载按钮时,会提示下载所有文件和我的应用程序以下载文件。我选择了我的应用程序,一切正常!!

这是某种安全性问题吗? 以下是我的Intent过滤器:

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

编辑:这是完整的活动标签。

  <activity android:name=".TestApp"
              android:label="@string/app_name" 
              android:configChanges="orientation|keyboardHidden"
              android:launchMode="singleTask">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="com.TestApp.TestApp.NOTIFICATION" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        <!-- Opening .ate file start -->
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:scheme="file" />
            <data android:host="*" />
            <data android:pathPattern=".*\\.ate" />
          </intent-filter>
    <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="*/*" />
         <data android:scheme="content" android:host="*"
            android:pathPattern=".*\\.ate" />
        <data android:scheme="file" android:host="*"
            android:pathPattern=".*\\.ate" />
    </intent-filter>
    <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="file" android:pathPattern=".*\\.ate" android:mimeType="application/octet-stream"/>
        <data android:scheme="content" android:pathPattern=".*\\.ate" android:mimeType="application/octet-stream"/>
        </intent-filter>
        <!-- Opening .ate file end  -->
    </activity>

你尝试过 android:host="gmail-ls" 吗? - IgorGanapolsky
1个回答

9
我自己想出了解决方法,所以在这里发布这个方案,以防其他人遇到这个奇怪的问题。
意图过滤器需要内容和文件方案类型,具有 mimetype 应用程序/octetstream。
<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="file" android:pathPattern=".*\\.inform" android:mimeType="application/octet-stream"/>
<data android:scheme="content" android:pathPattern=".*\\.inform" android:mimeType="application/octet-stream"/>


嗨 Blue,你能分享完整的<activity>标签吗? - Giuseppe
3
我已尝试了这个解决方案并且它是有效的,但它是具有误导性的,因为 pathPattern 将被忽略。除非过滤器同时指定了 schemehost 属性,否则将会忽略 pathPattern 属性(您的答案缺少 host 属性)。请参阅 文档 以获取有关 pathPattern 属性的详细信息。我在实践中也看到了这一点。意图过滤器将处理任何附件,而不仅仅是指定文件扩展名的附件。 - Jacob Adams
那么 <category android:name="android.intent.category.OPENABLE"/><action android:name="android.intent.action.GET_CONTENT" /> 呢? - IgorGanapolsky
@Manju,为什么不在你的答案中放置完整的解决方案,而是放在问题中呢?那样不更有意义吗?另外,为什么你在答案中使用了不同的扩展名? - ataravati

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接