Acr*_*les 2 android file android-intent
我有一个仪器化的电子书阅读器应用程序,可以用两种不同的方式打开它,一种方法是通过另一个应用程序,该应用程序直接从服务器下载内容,然后启动阅读器。另一种是通过从文件系统打开电子书。前者效果很好。后者适用[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>
Run Code Online (Sandbox Code Playgroud)
这并不是说它无法正常启动,而是因为该应用程序甚至没有出现在可能打开相应类型文件(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 File Manager打开它时(我的应用程序正确启动)
这是因为该应用程序甚至没有显示在打开相关类型文件的可能应用程序列表中(epubs)
那是因为您仅支持该file方案。
如果查看失败的Uri(content://com.android.externalstorage.documents/document/primary:Download/test.epub),您会注意到它具有content方案,而不是file方案。
如果您想支持该content计划,请更改<intent-filter>为:
<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>
Run Code Online (Sandbox Code Playgroud)
然后,使用ContentResolver和openInputStream()读取由标识的内容Uri,因为这对于file和content方案均适用。