Cannot Open File in Assets Error

The following error happens when trying to open a file in assets.
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.

Fix

  1. Change the file extension. aapt by default will not compress files with such extensions.

    1
    2
    3
    4
    5
    6
    ".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"
  2. Alternatively, you can disable compression for a specific extension. Add the configuration to build.gradle (Module: app)

    1
    2
    3
    4
    5
    android {
    aaptOptions {
    noCompress "pdf"
    }
    }

References

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