在应用安装/打开时注册新文件类型/扩展名 [英] Register new file type/extension on app install/open

查看:82
本文介绍了在应用安装/打开时注册新文件类型/扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先让我说我已经在这里阅读了一些类似的问题,对于例如,但只能部分解决我的问题,并且仍然有疑问.

Let me first say I've been reading some similar questions here, for example this one, but was only able to solve my issue partially and still having doubts.

我不确定自定义文件注册在Android中如何工作,我不知道是否可以在应用安装时注册新文件类型,或者只能在将打开文件,其次,我不确定是否有可能将您自己的自定义文件与您的应用程序自动关联,或者只有用户才能做到这一点.

I'm not sure how custom file registration works in Android, 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, and second, I'm not quite sure if there is any possibility to automatically associate your own custom file with your app, or this is something that only the user can do.

也就是说,我正在创建一个自定义文件类型(.aw扩展名),可以通过我的应用程序打开该文件,并使用下一个意图过滤器,可以从文件浏览器中打开该文件,例如,在我的Android 9华为中,但是它根本无法与其他设备一起使用,例如我的旧版Android 5平板电脑(单击文件时显示无法打开文件")或我的朋友Android 9 Samsung.我说这部分地"解决了,因为它似乎不是通用的工作意图过滤器.

That said, I'm creating a custom file type (.aw extension) to be opened with my app and with the next intent-filter I can open the file from the file explorer -for example- in my Android 9 Huawei, but it's not working at all with other devices, like my old Android 5 tablet (which says "Cannot open file" on file click) or my friend Android 9 Samsung. I said that solved "partially" because it appears not to be a universal working intent-filter.

如何找到注册自定义文件类型的所有Android版本"的工作方式,并让我知道是否有可能在您在资源管理器中或任何地方看到.aw文件时自动完成注册.图标出现,您的应用会打开文件.

How can I find a "all Android versions" working way of registering a custom file type and let me know if there is any possibility that registration is automatically done so whenever you see a .aw file in explorer -or wherever- your icon appears and your app opens the file.

这是我在manifest.xml中的意图过滤器

This is my intent-filter in 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>

这是我在打开文件的活动中对待它的方式:

And this is how I treat it in activity that will open the file:

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
    ...

推荐答案

我不确定自定义文件注册在Android中如何工作

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

它从未特别出色地工作过,并且在现代版本的Android上几乎毫无意义.确保您还有其他方式供用户选择要在您的应用中使用的内容,例如触发ACTION_OPEN_DOCUMENT Intent的打开"工具栏按钮.

It has never worked especially well, and it is nearly pointless on modern versions of Android. Make sure that you have other ways for the user to choose content for use in your app, such as an "open" toolbar button that triggers an ACTION_OPEN_DOCUMENT Intent.

我不知道是否可以在应用安装时注册新文件类型,或者只能在将打开文件的活动中使用Intent过滤器来完成

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>上继续显示.

It can only be done with an <intent-filter>. That typically goes on the <activity> that will handle the Intent. It could go on an <activity-alias> that you enable/disable separately, if there is some need for this.

我不太确定是否有可能将您自己的自定义文件与您的应用自动关联,或者只有用户才能做到这一点

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.

Having the <intent-filter> is automatic. Few apps will be creating a matching Intent, though.

我可以从文件浏览器中打开文件,例如在我的Android 9 Huawei中打开文件

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

大概这是该设备中的错误.在人类历史上大约有零个应用程序会知道您的非官方(可能是无效的)application/aw MIME类型,以创建使用它的Intent.

Presumably, that is a bug in that device. Approximately zero apps written in human history will know about your unofficial (and arguably invalid) application/aw MIME type to create an Intent that uses it.

如何找到注册所有自定义文件类型的所有Android版本"工作方式

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

没有.

只要您在资源管理器中看到一个.aw文件-或出现在任何位置-您的图标都会由您的应用打开该文件

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

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

There is no guaranteed way to do this. How a file explorer handles files is up to the developers of the file explorer, not you. Many will only work for common and popular MIME types, not others.

欢迎您使用pathPattern添加<intent-filter>来尝试在文件扩展名上进行字面上的匹配.由于Uri不必具有文件扩展名,因此并非在所有情况下都可以使用,但是它对某些用户和某些文件浏览器有效.

You are welcome to add an <intent-filter> using pathPattern to try to match literally on file extensions. Since a Uri does not have to have a file extension, this will not work in all cases, but it will work for some users and some file explorers.

通常,将这种向应用程序获取内容的形式视为一项附加功能,只有一小部分用户可以使用.主要集中于其他任何事物,例如ACTION_OPEN_DOCUMENT,这对每个人都适用.

In general, consider this form of getting content to your app to be an additional feature, usable by a small percentage of your users. Focus primarily on something else, such as ACTION_OPEN_DOCUMENT, that will work for everyone.

这篇关于在应用安装/打开时注册新文件类型/扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆