使用cordova-plugin-file读取大文件

时间:2017-11-03 10:50:38

标签: android cordova-plugin-file

我正在尝试在Android上的cordova应用程序中读取一个大文本文件。该文件大约80MB。由于内存限制,我决定阅读本文 以块的形式存档。 经过一些研究我已经指出,file.slice应该做的魔术。但它似乎不起作用。切片后返回的blob与整个文件的大小相同。我的期望是一个较小的blob,可由Android处理。

这是我使用的代码的摘录:

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: ');
...

这是Logcat生成的输出:

[#]文件切片:86998921 [#]文件成功:

然后,由于内存不足,应用程序崩溃了:

11-03 10:37:40.382 4156-4214 / com.my.app E / AndroidRuntime:FATAL EXCEPTION:pool-1-thread-3                                                                处理:de.knova.adisys,PID:4156                                                                java.lang.OutOfMemoryError:无法分配231997140字节分配4194304空闲字节和127MB直到OOM                                                                    at java.lang.StringFactory.newStringFromBytes(StringFactory.java:185)                                                                    at java.lang.StringFactory.newStringFromBytes(StringFactory.java:63)                                                                    在org.apache.cordova.file.FileUtils $ 26.handleData(FileUtils.java:1095)                                                                    在org.apache.cordova.file.Filesystem.readFileAtURL(Filesystem.java:284)                                                                    在org.apache.cordova.file.FileUtils.readFileAs(FileUtils.java:1066)                                                                    在org.apache.cordova.file.FileUtils $ 6.run(FileUtils.java:327)                                                                    在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)                                                                    在java.lang.Thread.run(Thread.java:761)

有人知道我做错了什么或者我怎么能读取这些大文件 在科尔多瓦?读取1024字节Blob会很好。

0 个答案:

没有答案