nailer nailer - 4 years ago 110
Android Question

getStartOffset returns wrong value of file in assets folder after upgrade of assembly conditions

The method

getStartOffset()
does not work correctly after I upgraded assembly conditions.


Following fragment of code worked fine to build app with api 19 (Android Plugin 1.5.0 + Gradle version 2.2.1), but when I try do same with api 25 and other building conditions (Android Plugin 2.2.0 + Gradle version 2.14.1),
getStartOffset()
returns value bigger length of file. Returned length is true.
In fact, the reading "png"-file isn't a image, I gave it extension PNG according to the requirements of aapt.

AssetFileDescriptor in = null;
FileInputStream fin = null;
FileChannel channel = null;
try {
in = context.getAssets().openFd(file); // .png
fin = in.createInputStream();
channel = fin.getChannel();
int length = (int) in.getLength(); // channel.size();
int offSet = (int) in.getStartOffset();
MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY,
0, length + offSet);
...
...
}


What is the problem? Thnks

Answer Source

This is normal behavior. I had to look at the contents of APK file to hash editor. To my surprise, my PNG file appeared twice included in the APK-file: its first entry offset from the start was 52 bytes, in the second case, the startOffset was more than 2MB. I.e. StartOffset is the shift in the apk-package.

Excitement was in vain. Everything is working:

MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, offSet,   length);
int spos = 0;
while(spos < length) {
 ......
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download