How can I use openRawResourcesFd()?

后端 未结 1 1499
滥情空心
滥情空心 2021-01-24 21:55

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

AssetFileDescriptor afd = getResources().openRawResourcesFd();

This a

相关标签:
1条回答
  • 2021-01-24 22:42

    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"
    };
    

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

    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>   
    

    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"> 
    

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

    0 讨论(0)
提交回复
热议问题