- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 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/
我正在尝试通过这样做来创建输入流 InputStream is = (InputStream) getResources().openRawResource(R.drawable.image1); 但
我有一个允许用户键入文件名的 editText,但由于所有文件都在原始文件夹中。我试图从原始文件夹中获取文件,但问题出在这里, String fileName = editText.getText()
我在 res/raw 中有文件,我想这样打开: AssetFileDescriptor afd = getResources().openRawResourcesFd(); 例如,这允许通过 afd.
我想在我的 Android 应用程序中加载一个 keystore 。我已将 key (truststore.bks) 复制到文件夹 res/raw 中。来自 Android 的 SDK 生成带有 tr
我必须在 java/eclipse/android 上使用以下代码读取名为 hello.txt 的文件: import java.io.InputStream; public class Tokeni
我正在使用 plist 库加载一个 plist 文件:http://code.google.com/p/plist/我正在使用这样的代码: //InputStream ins = getResourc
当应用程序首次运行时,我试图从我的 Android 项目中的 res/raw 文件夹加载一个 XML 文件。我用这一行来做到这一点: InputStream xmlStream = getResour
我在 res/raw/ 文件夹中有一个数据库文件。我正在调用 Resources.openRawResource(),文件名为 R.raw.FileName,我得到一个输入流,但我在设备中有另一个数据
在我的 android 应用程序中,我想以静态方法访问我的文本文件,但使用以下代码: InputStream = Resources.getSystem().openRawResource(R.raw
我可以使用 ClassLoader 加载放在 res/raw/ 文件夹中的 apk 吗?为了加载 apk,我使用 openRawResources()。 最佳答案 我不知道您是否可以从 Assets
我通过对谷歌的研究发现,我可以通过将文本文件存储在我的 res/raw 文件夹中然后通过 getResources().openRawResource(R.raw.words); 访问它来读取文本文件
在 MainActivity 类中,为什么我的注释行出现空指针异常 private Context context; public MainActivity(Context con) { su
android studio 2.1. preview 4 我正在创建一个 junit4 单元测试 来测试打开原始目录中包含的文件。 但是,每次代码运行时,我都可以从 openRawResource
基于 1.6 SDK4 InputStream is = Resources.getSystem().openRawResource(R.raw.vortexrules); 这条线给我带来了无尽的麻烦
//Reading a image file from @drawable res folder and writing to a file on external sd card //below o
我是一名优秀的程序员,十分优秀!