为什么我的“content://”URI返回一个空的InputStream,即使我可以从ZipResourceFile对象读取?

问题描述 投票:0回答:1

我正在设置Google的APEZProvider,以便从APK扩展zip文件中读取.PNG(压缩)和.MP3(未压缩)。如果我避开URI,文件检索工作正常,并坚持“getAPKExpansionZipFile()”但是当我尝试使用“content://”API检索我的文件时,我得到一个空的InputStream。 (我需要URI来满足媒体播放器的API。)

这适用于针对API 26的Android应用。过去,我能够让内容提供商工作。但我已经破坏了一些东西。我想知道模块版本不同步是否存在问题。

我试过通过APEZProvider(Google为这种情况提供的ContentProvider的子类)跟踪断点。应用程序找到提供者O.K.没有任何logcat错误。所以我认为我的清单设置是正确的。

起初,APEZProvider似乎初始化O.K.它甚至可以找到zip文件并正确填充mAPKExpansionPack私有变量。但不知何故,当它返回一个InputStream(在ContentProvider超类中)时,我得到null。

在我的存储库中:

// This works fine
fun makeSureWeCanReadFromPack() {
     val stream = 
mExpansionPack?.getInputStream("images/species_bubo_bubo.png")
     val bitmap = BitmapFactory.decodeStream(stream)
     if (bitmap == null) {
        // This doesn't happen
        throw DodgyFileException("Couldn't read test image")
     }
}

// This throws an error
fun makeSureContentProviderIsUpAndRunning() {
    val uri = Uri.parse("content://com.company.app.provider.ZipFileContentProvider/images/species_bubo_bubo.png")
    val stream = mContext.getContentResolver().openInputStream(uri)
    val bitmap = BitmapFactory.decodeStream(stream)
    if (bitmap == null) {
        // This happens
        throw DodgyFileException("Couldn't read from content provider")
    }
}

在我的Android清单中:

<provider android:authorities="com.company.app.provider.ZipFileContentProvider"
android:name="com.company.app.provider.ZipFileContentProvider"
android:exported = "true"
android:multiprocess = "true"
android:enabled = "true"
>
</provider>

编辑:这是内容提供商:https://pastebin.com/UjMjtYCD

android inputstream apk-expansion-files
1个回答
0
投票

解决方案:我需要更改我的ZIP文件,以便它在没有压缩的情况下构建,使用

zip -Z store

一个很大的线索是当我意识到Android可以从压缩的.PNG获得输入流,而不是资产文件描述符。

文件暗示了这一要求,但事实有点难以捉摸。我错误地将其建议解释为意味着在处理具有偏移和持续时间的播放媒体(例如歌曲和电影)时,您只需要避免压缩。但事实证明,压缩也会干扰检索图形文件。

© www.soinside.com 2019 - 2024. All rights reserved.