在应用 install/open 上注册新文件 type/extension

Register new file type/extension on app install/open

首先让我说一下,我一直在这里阅读一些类似的问题,for example this one,但只能部分解决我的问题并且仍然有疑问。

我不确定自定义文件注册在 Android 中是如何工作的,我不知道新文件类型是否可以在应用安装时注册,或者只能通过 intent-filter 完成activity 将打开文件,其次,我不太确定是否有可能自动将您自己的自定义文件与您的应用相关联,或者这是只有用户可以做的事情。

就是说,我正在创建一个自定义文件类型(.aw 扩展名)以使用我的应用打开,使用下一个 intent-filter 我可以从文件资源管理器打开文件 - 例如 - 在我的 Android 9 华为,但它根本无法与其他设备一起使用,例如我的旧 Android 5 平板电脑(点击文件时显示 "Cannot open file")或我的朋友 Android 9 三星。我说这解决了 "partially" 因为它似乎不是一个通用的工作意图过滤器。

我怎样才能找到注册自定义文件类型的 "all Android versions" 工作方式,并让我知道是否有可能自动完成注册,以便在您在资源管理器中看到 .aw 文件时或在任何地方- 您的图标出现并且您的应用打开文件。

这是我在 manifest.xml

中的意图过滤器
<intent-filter
  android:icon="@drawable/ic_launcher"
  android:label="@string/app_name">
  <action android:name="android.intent.action.VIEW" />
  <category android:name="android.intent.category.DEFAULT" />
  <data
      android:host="*"
      android:mimeType="application/aw"
      android:scheme="content" />
</intent-filter>

这就是我在 activity 中的处理方式,它将打开文件:

private String[] getIntentExtras()
{
    Intent intent = getIntent();
    String fileContents = null;
    String action = intent.getAction();
    if(action!=null)
    {
        if (action.equals(Intent.ACTION_VIEW))
        {
            fileContents = AWImport.importData(intent);
            if (fileContents.startsWith("whatever"))
            {
                fileContents = fileContents.replace("whatever", "");
            }
            else
            {
                showErrorOpeningFile();
                return null;
            }
        }
    }
    else
    ...

I'm not sure how custom file registration works in Android

它从来没有特别好用,而且在 Android 的现代版本上几乎毫无意义。确保您有其他方式供用户选择要在您的应用中使用的内容,例如触发 ACTION_OPEN_DOCUMENT Intent.

的 "open" 工具栏按钮

I don't know if a new file type can be registered on app install or it can only be done with an intent-filter in the activity that will open the file

只能用 <intent-filter> 来完成。这通常会在处理 Intent<activity> 上进行。如果有需要,它可以继续 <activity-alias>,你 enable/disable 分开。

I'm not quite sure if there is any possibility to autommatically associate your own custom file with your app, or this is something that only the user can do

<intent-filter> 是自动的。不过,很少有应用会创建匹配 Intent

I can open the file from the file explorer -for example- in my Android 9 Huawei

据推测,这是该设备中的一个错误。人类历史上大约有零个应用程序会知道您的非官方(并且可以说是无效的)application/aw MIME 类型以创建使用它的 Intent

How can I find a "all Android versions" working way of registering a custom file type

有none.

whenever you see a .aw file in explorer -or wherever- your icon appears and your app opens the file

没有保证的方法可以做到这一点。文件资源管理器如何处理文件取决于文件资源管理器的开发人员,而不是您。许多只适用于常见和流行的 MIME 类型,不适用于其他类型。

欢迎您使用 pathPattern 添加 <intent-filter> 以尝试按字面匹配文件扩展名。由于 Uri 不必有文件扩展名,这并非在所有情况下都有效,但它适用于某些用户和某些文件浏览器。

一般来说,将这种将内容获取到您的应用程序的方式视为一项附加功能,供一小部分用户使用。主要关注其他对每个人都适用的东西,例如 ACTION_OPEN_DOCUMENT