【问题标题】:Custom file extension doesn't open自定义文件扩展名无法打开
【发布时间】:2019-08-28 22:09:09
【问题描述】:

之前问过同样的问题,我测试了所有问题,但没有一个有效。
我在一个文件中写了一个序列化对象,我命名为575454.myCustomExe
我希望我的应用程序只打开 *.myCustomExe 而不是更多。

我如何保存文件:

    File file = new File(Environment.getExternalStoragePublicDirectory("MyAppFolder") , getId() + ".myCustomExe");
    if (!file.exists()) {
        file.getParentFile().mkdirs();
        file.createNewFile();
        }
    ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));
    out.writeObject(serializableObject);
    out.close();

我的意图过滤器:

        <activity
            android:name=".ui.activity.ImportActivity">

            <intent-filter android:icon="@drawable/ic_save"
                android:label="label"
                android:priority="1">
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />    

                <data android:scheme="content" />
                <data android:host="*" />
                <data android:mimeType="*/*" />
                <data android:pathPattern=".*\.myCustomExe"/>    

            </intent-filter>

我也试过&lt;data android:pathPattern=".*\\.myCustomExe" /&gt;。

【问题讨论】:

    标签: android file mime-types intentfilter


    【解决方案1】:

    文件扩展名在 Android 上使用不多。从 Android Q 开始,files 在 Android 上很少使用。 ContentProvider 不需要在 content Uri 上放置类似文件的扩展名。所以,虽然你所拥有的我认为在技术上是正确的,但正如你所指出的,它不会很好地工作。

    对此您无能为力,因为您无法更改其他应用的行为。

    如果您希望支持常见的Intent 操作,例如ACTION_VIEW,最好的办法是将文件保存为具有匹配文件扩展名的常见元格式(例如 JSON、XML),然后将您的 @ 987654326@ 过滤相应的 MIME 类型。您需要处理用户选择的文件不是由您的应用创建的文件的可能性,尽管从技术上讲,即使使用您的自定义扩展,您也需要处理这种情况。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-14
      • 2016-11-12
      • 1970-01-01
      相关资源
      最近更新 更多