cordova-plugin-file icon indicating copy to clipboard operation
cordova-plugin-file copied to clipboard

Android: Large file read error

Open matrix0123456789 opened this issue 5 years ago • 6 comments

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),

matrix0123456789 avatar Aug 08 '18 06:08 matrix0123456789

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?

janpio avatar Aug 08 '18 08:08 janpio

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.

matrix0123456789 avatar Aug 08 '18 10:08 matrix0123456789

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)

janpio avatar Aug 08 '18 10:08 janpio

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.

manoj529 avatar Dec 13 '18 11:12 manoj529

I've posted a link to a workaround with readAsArrayBuffer for the chunk size problem in #284.

chauthai avatar Dec 14 '18 15:12 chauthai

Any update on this ?

saikiranjamiligiri avatar Jan 27 '20 11:01 saikiranjamiligiri