如何使用openRawResourcesFd()?如何使用、openRawResourcesFd

2023-09-04 03:25:55 作者:枯骨

我在文件 RES /生,我想开这样的:

I have files in res/raw that I would like to open like this:

AssetFileDescriptor afd = getResources().openRawResourcesFd();

这允许,例如,通过 afd.getLength得到明确的文件大小(),其中 InputStream.available()不答应,即使它的似乎的普遍工作。

This allows for, e.g., getting the definitive file size via afd.getLength(), which InputStream.available() does not promise, even if it seems to generally work.

然而,当我尝试这一点,将失败:

However, when I try this it fails:

java.io.FileNotFoundException:此文件无法打开的文件   描述;它可能是COM pressed

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

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

These are just small text files. What's the problem?

推荐答案

AAPT COM preSS最多的资源作为构建过程的一部分。据这篇文章它引用的是AAPT来源,例外情况是:

aapt compress most resources as part of the build process. According to this article which quotes the aapt source, the exceptions are:

/* 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。

So one way to get around this problem is to spoof aapt by renaming your files with one of those extensions.

这似乎不是很理想。相反,如果你想添加扩展到例外列表中,您可以编辑 [SDK] /tool​​s/ant/build.xml 。搜索文件 nocom preSS

That may not seem very satisfactory. If instead you want to add an extension to the list of exceptions, you can edit [sdk]/tools/ant/build.xml. Search the file for 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 。请注意,你也可以关闭COM pression一起。

The comments at the bottom are self-explanatory. I inserted the exception for txt. Notice you can also turn off compression all together.

另外,创建一个特殊的扩展:

Alternately, create a special extension:

<nocompress extension="nocomp"> 

和重命名文件,例如, whatever.txt.nocomp 。这样你就可以留下的一切,除了特定的文件COM pression。

And rename your files, e.g., whatever.txt.nocomp. This way you can leave the compression on for everything except particular files.