【问题标题】:Android "can't open file" from certain file managersAndroid 从某些文件管理器“无法打开文件”
【发布时间】:2017-02-22 16:28:02
【问题描述】:

我有一个经过检测的电子书阅读器应用程序,它可以通过两种不同的方式打开,一种方式是通过另一个应用程序直接从服务器下载内容,然后启动阅读器。另一种是从文件系统打开电子书。前者工作得很好。后者有效,[i]但仅适用于特定的文件管理器[/i]。它适用于 Astro 和 ES 文件管理器。它不适用于默认的 Android 文件管理器或 AndExplorer(这显然不是详尽的测试)。

这是清单中的相关部分:

<activity
            android:name=".activities.IntentResolverActivity"
            android:label="@string/app_name"
            android:configChanges="orientation|screenSize"
            android:windowSoftInputMode="stateHidden|adjustPan"
            android:theme="@android:style/Theme.Holo.Light.NoActionBar"
            android:exported="true"
            android:screenOrientation="landscape" >

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

并不是说它没有正确启动,而是该应用程序甚至没有出现在可以打开相关类型文件 (epub) 的可能应用程序列表中。我敢打赌,它与 mime 类型有关,并且这不起作用的文件管理器对 mime 类型有一些神奇的作用。但我特别说我不关心 mimetypes ("/") 以避免任何问题,而且我似乎无法找到有效的 mime 类型(我什至不知道是什么)它可能不是八位字节流:epub 本质上是一个 zip 文件,如果我尝试 mimetype application/zip 它会破坏所有文件管理器)。

在 Android Marshmallow 和 Nougat 中进行了测试,我不知道在 Kitkat 中是否会发生同样的情况(据我所知,旧版 Android 甚至没有内置文件管理器)。

我已尝试完全删除 mime 类型(不起作用),还尝试将所有数据条目压缩为一个,这与将它们分开一样。由于我不想强迫用户使用“已批准”的文件管理器,我需要解决这个问题,但不知道出了什么问题。

以下是意图的 logcat 消息:

I/ActivityManager: START u0 {act=android.intent.action.VIEW dat=content://com.android.externalstorage.documents/document/primary:Download/test.epub typ=application/epub+zip flg=0x3 cmp=android/com.android.internal.app.ResolverActivity} from uid 10042 on display 0

来自 Android 文件管理器(请注意,我的阅读器并未显示为可能启动的应用之一)并且:

/ActivityManager: START u0 {act=android.intent.action.VIEW dat=file:///storage/emulated/0/Download/test.epub typ=application/epub+zip flg=0x10000000 cmp=org.fictitious.epubreader/.activities.IntentResolverActivity} from uid 10114 on display 0

当我从 ES 文件管理器打开它时(并且我的应用程序正确启动)

【问题讨论】:

    标签: android file android-intent


    【解决方案1】:

    应用程序甚至没有出现在可能打开相关类型(epub)文件的应用程序列表中

    那是因为你只支持file方案。

    如果您查看失败的Uri (content://com.android.externalstorage.documents/document/primary:Download/test.epub),您会注意到它具有content 方案,而不是file 方案。

    如果您想支持content 方案,请将您的&lt;intent-filter&gt; 更改为:

             <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="file" />
                <data android:scheme="content" />
                <data android:mimeType="application/epub+zip"/>
            </intent-filter>
    

    然后,使用ContentResolveropenInputStream() 读取Uri 标识的内容,因为这适用于filecontent 方案。

    【讨论】:

    • 是的,做到了。谢谢!为了完整起见,我没有完全使用那个意图过滤器。我将它分成两个意图过滤器,一个用于方案文件,一个用于方案内容,因此我可以打开 epub 类型的文件,而不管 mimetype 是什么,但其余的我按照你的建议做了,效果很好。
    猜你喜欢
    • 2019-12-09
    • 1970-01-01
    • 2012-03-26
    • 1970-01-01
    • 2019-03-08
    • 2015-05-10
    • 2022-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多