我想将特定扩展名的文件(例如任何扩展名为 .xyz 的文件)与我的应用程序关联。这意味着,当用户在文件资源管理器(或其他地方,如 gmail、驱动器等)中点击 SaveFile1.xyz 时,我的应用程序应该启动,并且文件的 Uri 将在 Intent 中传递给我的 Activity。
虽然这是预期的行为,但我不知道如何关联自定义文件。我已阅读并理解如何意图匹配 https://developer.android.com/guide/components/intents-filters#Resolution的工作原理和可能的值data https://developer.android.com/guide/topics/manifest/data-element标签,但是没用。我检查了一些 stackoverflow 答案(例如,this https://stackoverflow.com/questions/3760276/android-intent-filter-associate-app-with-file-extension and this https://stackoverflow.com/questions/27547866/custom-file-extension-in-android),但它们使用文件方案和路径模式。
我们不能使用pathPattern,因为我们得到一个内容uri(带有scheme=content的uri),它是抽象路径并且不会有任何扩展。
mime 类型设置只能用于预定义值(例如 image/png 可以将所有 png 图像与应用程序关联)。
我可以将 mime 类型设置为“*/*”,但我不希望我的应用程序接受每个文件(然后在 onCreate 或其他内容中拒绝它)。
有人解决这个问题了吗?任何帮助将不胜感激。
我在清单文件的活动标记中有以下内容。
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<!--<data android:mimeType="application/pdf"/>
<data android:mimeType="image/png"/>-->
<!--<data android:scheme="content" android:mimeType="*/*"/>-->
</intent-filter>
</activity>
编辑1:
我将 mime 类型设置为“*/*”,并在 Activity.onCreate 中添加以下行以查看意图中的类型(并将其设置在清单文件中)...
Intent dataIntent = getIntent();
Log.i(TAG, "dataIntent.getType() = " + dataIntent.getType());
....但得到以下输出。
dataIntent.getType() =
它甚至不为空。