当前位置: 代码迷 >> XML/SOAP >> android使用build.xml配置assert文件中不压缩的门类文件
  详细解决方案

android使用build.xml配置assert文件中不压缩的门类文件

热度:1268   发布时间:2013-12-29 13:07:03.0
android使用build.xml配置assert文件中不压缩的类型文件

问题:

在做文本文件读取时,从assert文件中获取文件

getResources().getAssets().openFd("test.txt").getFileDescriptor()

会报错,提示File not Found ,because the file compressed,查找了很多资料,android打包成apk时,在assert文件中,除了音视频文件不压缩外,像txt之类的会进行压缩成zip包,以节约空间,所以getFileDescriptor()时获取不到,

通用都是获取文件流,但在做大文件读取时,最新技术推荐使用java nio中内存映射技术MappedByteBuffer

?

public void openbook(FileDescriptor fileD) throws IOException {
??
??//book_file = new File(strFilePath);
??FileInputStream in=new FileInputStream(fileD);
??long lLen = in.available();
??m_mbBufLen = (int) lLen;
??m_mbBuf =in.getChannel().map(
????FileChannel.MapMode.READ_ONLY, 0, lLen);
?}

?问题参考:

http://ponystyle.com/blog/2010/03/26/dealing-with-asset-compression-in-android-apps/

When developing an Android app, any data file, image or XML file (that is, any Resource or Asset) you use is bundled into your application package (APK) for distribution. The Android Asset Packaging Tool, or?aapt, is responsible for creating this bundle, which you can think of as a ZIP file with a particular layout that the Android OS can understand. When your application is installed, whether in development mode or by an end user, this APK file is simply dropped into a special location on the device’s (or emulator’s) filesystem.

As part of preparing your APK,?aapt?selectively compresses various assets to save space on the device. The way?aapt?determines which assets need compression is by their file extension. The following code, taken from?Package.cpp?in the?aaptsource code, sheds some light on which types of files are not compressed by default:

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

?

?

解决方案:

配置build.xml时配置不压缩txt文件

D:\android-eclipse\sdk\tools\ant

<!-- Puts the project's resources into the output package file
???????? This actually can create multiple resource package in case
???????? Some custom apk with specific configuration have been
???????? declared in default.properties.
???????? -->
??? <target name="-package-resources" depends="-crunch">
??????? <!-- only package resources if *not* a library project -->
??????? <do-only-if-not-library elseText="Library project: do not package resources..." >
??????????? <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>
??????? </do-only-if-not-library>
??? </target>

?

?

?

?

?

  相关解决方案