cordova-plugin-file
cordova-plugin-file copied to clipboard
Android: Large file read error
When you read file by somethig like
var reader = new FileReader();
reader.onloadend=console.log;
reader.readAsText(file);
and if the file is bigger that CHUNK_SIZE (256kB) it gives you corrupted file: for example reading file that has about 0.5MB i get string, which length is over 800 000 characters (tested on android).
My solution, in www/FileReader.js instead of line
readSuccessCallback.bind(this, readType, encoding, offset, totalSize, accumulate),
put
readSuccessCallback.bind(this, readType, encoding, offset, Math.min(totalSize - this._progress, CHUNK_SIZE), accumulate),
Does this bug happen only on Android? What version of Android? Did you encounter the same problem in iOS?
What exactly does your solution do? Can you explain it in plain words?
When plugin exec java code, sends how many bytes to read. But it sends size of whole file. And when it recived from java code, it increments this._progress by CHUNK_SIZE
this._progress = Math.min(this._progress + CHUNK_SIZE, totalSize);
And then it asks java code (by exec function) for the rest of file, although it already has whole file.
I didn't tested on other platforms.
Could you please create a Pull Request with your change? (You can ignore the requirement for JIRA issue when creating the PR - we are currently removing this)
Even i'm facing similar issue with android6.0 where im getting an error like "Failed to allocate a ***** byte allocation with 1243880 free bytes and 24MB until OOM.... the issue is getting resolved when i include a parameter android:largeHeap="true" in android manifest file. Is this bug resolved? As we are expecting the file is going to be much more larger in future (Ex.20-24MB JSON file) in future for our applications.
I've posted a link to a workaround with readAsArrayBuffer for the chunk size problem in #284.
Any update on this ?