微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Phonegap android文件到base64内存不足错误

我试图在Android获取一个视频文件,将其转换为base64编码并上传它.

文件大于5Mb时,我在android中出现内存错误,但ios也会转换大文件.只在android中我得到了这个错误….

这是我的代码

var reader = new FileReader();
reader.onload = function(evt1) {}, reader.onloadend = function(evt) {
    console.log("read success");
    console.log(evt.target.result);
};
reader.readAsDataURL(file);

解决方法:

>您应该知道base64编码的数据将比原始数据大小大约37%.考虑到你正在处理大文件和base64编码导致内存不足错误,我不会用base64编码它.
>一次读取大文件不是一个好主意.相反,我建议将您的文件流式传输到服务器.使用此方法,将以块的形式读取和传输文件,防止内存不足错误并避免较慢设备上的滞后.例如,可以使用FileUploadOptions中启用了chunkedMode的FileTransfer对象来执行此操作.

推荐方法(改编自documentation):

// !! Assumes variable fileURI contains a valid URI to a H.264/AVC video on the device

var win = function (r) {
    console.log("Code = " + r.responseCode);
    console.log("Response = " + r.response);
    console.log("Sent = " + r.bytesSent); }

var fail = function (error) {
    alert("An error has occurred: Code = " + error.code);
    console.log("upload error source " + error.source);
    console.log("upload error target " + error.target); }

var options = new FileUploadOptions();
options.fileName = fileURI.substr(fileURI.lastIndexOf('/') + 1);
options.mimeType = "video/avc"; //change to the according mimeType
options.chunkedMode = true; //upload the data in chunked streaming mode (true by default)

var ft = new FileTransfer();
ft.upload(fileURI, encodeURI("http://some.server.com/upload.PHP"), win, fail, options);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐