I'm trying to read a large Textfile inside a cordova app on Android. The file is about 80MB big. Because of memory restrictions I have decided to read this file in chunks. After some Research I have pointed out, that file.slice should do the magic. But it seems not to work. The blob returned after slice has the same size tha the whole file. My expectation was a smaller blob which can be handled by Android.
This is an excerpt of the code I use:
window.resolveLocalFileSystemURL(source, (fileSource: FileEntry) => {
console.log('[!] source found: ' + source);
fileSource.file((sourceFile: File) => {
let blob : Blob = sourceFile.slice(0, 128);
console.log('[#] file slice: ' + blob.size);
blob = blob.slice(0, 128);
let reader: FileReader = new FileReader();
reader.readAsDataURL(blob);
console.log('[#] file success: ');
...
This is the generated Output from Logcat:
[#] file slice: 86998921 [#] file success:
Then Application crashes because of not enough memory:
11-03 10:37:40.382 4156-4214/com.my.app E/AndroidRuntime: FATAL EXCEPTION: pool-1-thread-3 Process: de.knova.adisys, PID: 4156 java.lang.OutOfMemoryError: Failed to allocate a 231997140 byte allocation with 4194304 free bytes and 127MB until OOM at java.lang.StringFactory.newStringFromBytes(StringFactory.java:185) at java.lang.StringFactory.newStringFromBytes(StringFactory.java:63) at org.apache.cordova.file.FileUtils$26.handleData(FileUtils.java:1095) at org.apache.cordova.file.Filesystem.readFileAtURL(Filesystem.java:284) at org.apache.cordova.file.FileUtils.readFileAs(FileUtils.java:1066) at org.apache.cordova.file.FileUtils$6.run(FileUtils.java:327) at org.apache.cordova.file.FileUtils$25.run(FileUtils.java:663) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) at java.lang.Thread.run(Thread.java:761)
Does anybody know what I'm doing wrong or how I can read this large files in Cordova? It would be fine to read 1024 Byte Blobs.