PhoneGap的Andr​​oid文件为Base64内存不足的错误 [英] Phonegap android file to base64 out of memory error

查看:164
本文介绍了PhoneGap的Andr​​oid文件为Base64内存不足的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获得在Android上的视频文件,将其转换为base64编码并上传。

在文件大于为5Mb ,我得到的机器人 内存不足的错误,但IOS转换较大大文件也。只有在Android的我得到这个错误....

这是我的code:

  VAR读卡器=新的FileReader();
reader.onload =功能(EVT1){},reader.onloadend =功能(EVT){
    的console.log(读成功);
    的console.log(evt.target.result);
};
reader.readAsDataURL(文件);


解决方案

  1. 您应该知道,以base64恩codeD数据会比原来的数据大小约为37%。考虑到你正在处理大文件和的base64编码会导致内存不足的错误,我不会带code将其使用Base64。


  2. 这不是一次读取大型文件的好主意。相反,我建议你的文件流媒体服务器。通过这种方法,该文件将被读取并传输数据块,preventing内存不足的错误和较慢的设备,避免滞后。您可以使用文件传输对象, chunkedMode FileUploadOptions


推荐的方法(改编自<一个href=\"https://cordova.apache.org/docs/en/3.0.0/cordova_file_file.md.html#FileTransfer\">documentation):

  //!假定变量fileURI所包含一个有效的URI到设备上的H.264 / AVC视频VAR赢=功能(R){
    的console.log(code =+ r.response code);
    的console.log(响应=+ r.response);
    的console.log(已发送=+ r.bytesSent); }VAR失败=功能(错误){
    (发生错误:code =+错误code)警报;
    的console.log(上传错误源+ error.source);
    的console.log(上传错误目标+ error.target); }VAR的选择=新FileUploadOptions();
options.fileName = fileURI.substr(fileURI.lastIndexOf('/')+ 1);
options.mimeType =视频/ AVC //更改为根据mime类型
options.chunkedMode = TRUE; //上传分块流模式下的数据(默认为true)VAR英尺=新的文件传输();
ft.upload(fileURI所,连接codeURI(http://some.server.com/upload.php),胜利,失败,期权);

I am attempting to get a video file on android, convert it to base64 encoding and upload it.

When the file is larger than 5Mb, I get out of memory error in android, but ios convert large files also. Only in android I got this error....

This is my code:

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

解决方案

  1. You should be aware that the base64-encoded data will be roughly 37% larger than the original data size. Considering that you're dealing with large files and the base64-encoding causes out of memory errors, I would not encode it with base64.

  2. It's not a good idea to read large files at once. Instead, I recommend streaming your file to the server. With this approach, the file will be read and transferred in chunks, preventing out of memory errors and avoiding lags on slower devices. You can do this for example using the FileTransfer object with chunkedMode enabled in the FileUploadOptions.

Recommended approach (adapted from the 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);

这篇关于PhoneGap的Andr​​oid文件为Base64内存不足的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆