我找到了一种通过内容提供商来做到这一点的方法。我从问题中得到了有关内容提供商的提示android 在扩展文件中播放电影 https://stackoverflow.com/questions/13813185/android-play-movie-inside-expansion-file,尽管在该问题中 URL 并未用于启动外部应用程序。
我们需要一个扩展 Google 的 APEZProvider 的 ContentProvider 类:
public class ZipFileContentProvider extends APEZProvider {
@Override
public String getAuthority() {
return "com.example.expansionexperiment.provider";
}
}
并且需要将其添加到Manifest中并导出:
<provider
android:exported="true"
android:name="com.example.expansionexperiment.ZipFileContentProvider"
android:authorities="com.example.expansionexperiment.provider" />
清单中的权限和 getAuthority() 中的权限必须相同。
现在我们可以为扩展文件中的文件创建一个 Uri:
// Filename inside my expansion file
String filename = "video/video.mp4";
String uriString = "content://com.example.expansionexperiment.provider" + File.separator + filename;
Uri uri = Uri.parse(uriString);
// Start video view intent
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "video/*");
startActivity(intent);
内容提供商现在仅通过 URL 即可提供对 zip 文件和其中文件的访问。
但是,PhoneGap 的 Media 插件不支持内容提供商,因为如果 URL 不是以 http 开头,则会在 URL 前面插入“/sdcard/”。 (!!!) 因此,要通过 zip 内容提供程序播放音频文件,您需要编写自己的音频插件。