Java.Io.Filenotfoundexception: This File Can Not Be Opened as a File Descriptor; It Is Probably Compressed

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

There is a limitations on opening compressed files in the assets folder. This is because uncompressed files can be directly memory mapped into the processes virtual address space, therefore avoiding needing the same amount of memory again for decompression.

Dealing with Asset Compression in Android Apps discusses some techniques in dealing with compressed files. You can trick aapt into not compressing the file by using an extension that is not compressed (e.g. mp3) or you can manually add them to the apk without compression instead of getting aapt to do the work.

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

It turns out to be a problem with the NetBeans generated build scripts. I did a tutorial from scratch and it failed in NetBeans and worked in Eclipse. I suppose I will finally learn Eclipse. It is something I have been wanting to do at least as a comparison in hopes it would be less buggy, but I have not had the time. Well necessity is going to gave me the time now.

Android: getAssets().openFd() and FileNotFoundException

try this :

AssetFileDescriptor descriptor = getAssets().openFd("openAccess.txt");
BufferedReader f = new BufferedReader(new FileReader(descriptor.getFileDescriptor()));
String line = f.readLine();
while (line != null) {
// do stuff
Log.d("TAG",line);
}

FileReader csv : FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed

Android compresses all assets, except for the following types:

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

If you have sufficient control over the build process, you can use the
"-0" flag with zip or aapt to add the assets.

-0 specifies an additional extension for which such files will not
be stored compressed in the .apk. An empty string means to not
compress any files at all.

If you have the chance to do it with your CSVReader, deposit your csv file somewhere other than the assets folder, like sdcard.

How can I use openRawResourcesFd()?

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.

Android Development : File is probably compressed

So my problem is how do I fix the txt file not opening correctly and apprarently being compressed.

Instead of calling openFd(), call open(), to get an InputStream directly, and replace your FileReader with an InputStreamReader (if you really want a Reader interface).



Related Topics



Leave a reply



Submit