gpt4 book ai didi

android - 如何使用 openRawResource Fd()?

转载 作者:行者123 更新时间:2023-11-29 16:05:00 25 4
gpt4 key购买 nike

我在 res/raw 中有文件,我想这样打开:

AssetFileDescriptor afd = getResources().openRawResourcesFd();

例如,这允许通过 afd.getLength() 获取确定的文件大小,而 InputStream.available() 并没有 promise ,即使它 似乎通常有效。

但是,当我尝试这样做时,它失败了:

java.io.FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed

这些只是小文本文件。有什么问题?

最佳答案

aapt 在构建过程中压缩大部分资源。根据this article引用 aapt 源代码,异常(exception)情况是:

/* these formats are already compressed, or don't compress well */
static const char* kNoCompressExt[] = {
".jpg", ".jpeg", ".png", ".gif",
".wav", ".mp2", ".mp3", ".ogg", ".aac",
".mpg", ".mpeg", ".mid", ".midi", ".smf", ".jet",
".rtttl", ".imy", ".xmf", ".mp4", ".m4a",
".m4v", ".3gp", ".3gpp", ".3g2", ".3gpp2",
".amr", ".awb", ".wma", ".wmv"
};

因此解决此问题的一种方法是通过使用其中一个扩展名重命名文件来欺骗 aapt。

这似乎不是很令人满意。相反,如果您想向异常(exception)列表添加扩展,则可以编辑 [sdk]/tools/ant/build.xml。在文件中搜索 nocompress:

        <aapt executable="${aapt}"
command="package"
versioncode="${version.code}"
versionname="${version.name}"
debug="${build.is.packaging.debug}"
manifest="${out.manifest.abs.file}"
assets="${asset.absolute.dir}"
androidjar="${project.target.android.jar}"
apkfolder="${out.absolute.dir}"
nocrunch="${build.packaging.nocrunch}"
resourcefilename="${resource.package.file.name}"
resourcefilter="${aapt.resource.filter}"
libraryResFolderPathRefid="project.library.res.folder.path"
libraryPackagesRefid="project.library.packages"
libraryRFileRefid="project.library.bin.r.file.path"
previousBuildType="${build.last.target}"
buildType="${build.target}"
ignoreAssets="${aapt.ignore.assets}">
<res path="${out.res.absolute.dir}" />
<res path="${resource.absolute.dir}" />
<nocompress extension="txt"/>
<!-- <nocompress /> forces no compression on any files in assets or res/raw -->
<!-- <nocompress extension="xml" /> forces no compression on specific file extensions in assets and res/raw -->
</aapt>

底部的注释是不言自明的。我为 txt 插入了异常(exception)。请注意,您也可以一起关闭压缩。

或者,创建一个特殊的扩展:

<nocompress extension="nocomp"> 

并重命名您的文件,例如,whatever.txt.nocomp。这样您就可以对除特定文件之外的所有内容保持压缩。

关于android - 如何使用 openRawResource Fd()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19743169/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com