如何打开从电子邮件到我的应用程序的特定扩展文件?

How to open a particular extension file from the email to my app?

我的电子邮件中有一个附件。现在,一旦我单击附件,它就会打开,并在我的设备中安装我的特定应用程序。我怎样才能做到这一点?请帮助

将 intent-filter 添加到清单文件 activity。

<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:scheme="file" />
  <data android:mimeType="*/*" />
  <data android:pathPattern=".*\.txt" />
</intent-filter>

然后在 activity 检查意图的 onCreate() 中:

Intent intent = getIntent();
if(Intent.ACTION_VIEW.equals(intent.getAction())){
    // use getData(), etc...
    String name = intent.getData().getPath();
}

您也可以查看 this article。它适用于第 3 方应用程序与您的应用程序共享某些内容的情况(不适用于 "open with"),但它是相似的并且可能有助于理解 sharing/opening 的工作原理。