Java – read IOException from InputStream
When reading from InputStream on Android platform, I encountered a strange problem I don't know if this is an Android specific problem or if I'm doing something wrong
This is the only specific call:
InputStream is = getResources().openRawResource(R.raw.myfile);
This will return a file's InputStream from the Android asset Anyway, here is the problem I encountered:
bytes[] buffer = new bytes[2]; is.read(buffer);
IOException will be thrown when read() is executed Strangely, if I do two sequential single byte reads (or any number of single byte reads), it is no exception For example, do this:
byte buffer; buffer = (byte)buffer.read(); buffer = (byte)buffer.read();
Any idea why two sequential single byte reads work, but one call reads two and one throws an exception? InputStream seems good... Is Available() returns more than one million bytes
Stack trace in InputStream Show these lines before read():
java.io.IOException at android.content.res.AssetManager.readAsset(Native Method) at android.content.res.AssetManager.access$800(AssetManager.java:36) at android.content.res.AssetManager$AssetInputStream.read(AssetManager.java:542)
Changing the buffer size to a single byte still throws an error It looks like an exception occurs only when reading a byte array
If I truncate the file to 100000 bytes (the file is: it was 1917408 bytes), it will work normally Is there a problem with a file of a certain size?
Any help is appreciated! thank you!
Solution
(my post to Android developers didn't appear, so I'll try to republish it here)
IIRC, this problem comes from trying to access the file compressed when building APK
Therefore, to solve this problem, give it a file extension that will not be compressed I forgot what extensions were skipped on the list, but already compressed file types (such as MP3, JPG) may work