PhoneGap FileReader / readAsDataURL不触发回调 [英] PhoneGap FileReader/readAsDataURL Not Triggering Callbacks
问题描述
我使用PhoneGap Build建立iOS v7.1 +应用程式,并使用 weinre 来调试。我使用媒体捕获插件和文件API捕获视频,试图获得其base64表示。我可以得到录像机打开,拍摄视频,并返回文件路径。然后使用 resolveLocalFileSystemURL()
获取 readAsDataURL()
所需的文件对象。问题是 FileReader
从不调用 onloadend
回调。
I am using PhoneGap Build to build an iOS v7.1+ application and using weinre to debug. I am using the media-capture plugin and file API to capture a video in an attempt get its base64 representation. I can get the video recorder to open, take a video, and return the file path. I then use resolveLocalFileSystemURL()
to get a file object which readAsDataURL()
requires. The problem is FileReader
is never calling the onloadend
callback.
我一整天都在偷窥。把 console.log()
放在我想到的地方。我查看了以确保iOS版本受支持。每个变量是我所期望的,但回调只是不被调用。我也试过设置所有其他回调,但没有一个被调用。我试着用 readAsText()
替换 readAsDataURL()
,但我还是得到bupkis。我已经尝试等待五分钟,因为我认为异步调用可能需要一些,但仍然没有什么。
I have been poking around all day. Putting console.log()
's everywhere I could think of. I checked to make sure the iOS version is supported. Every variable is what I expect yet the callback is simply not being called. I have also tried setting up all the other callbacks but none of them ever get called, either. I have tried replacing readAsDataURL()
with readAsText()
but I still get bupkis. I have tried waiting up to five minutes since I figured an asynchronous call may take a bit but still nothing.
下面是我的代码。下面是控制台输出。
Below is my code. Below that is the console output.
var elements = new Object();
elements["video"] = $("#window_incident_create > .video > source")[0];
navigator.device.capture.captureVideo(
function(files) {
for ( var i in files ) {
var file = files[i];
var name = file.name;
var path = file.fullPath;
if ( path.indexOf("/private") === 0 )
path = "file://" + path.substr(8);
else
path = "file://" + path;
var type = file.type;
var lastModifiedDate = file.lastModifiedDate;
var size = file.size;
var reader = new FileReader();
reader.onloadend = function(event) {
console.log(3);
elements["video"].type = type;
elements["video"].src = "data:" + type + ";base64," + event.target.result;
console.log(4);
};
window.resolveLocalFileSystemURL(
path,
function(entry) {
console.log(1, entry.nativeURL);
reader.readAsDataURL(entry);
console.log(2);
},
function(error) {
console.log("0-0", error);
}
);
}
},
function(error) {
console.log("0-1", error);
},
{
limit: 1
}
);
1file:/// var / mobile / Applications / AB239984 -FB9F-43C0-B699-3596AC8A43A8 / tmp / capture / capturedvideo.MOV
1 "file:///var/mobile/Applications/AB239984-FB9F-43C0-B699-3596AC8A43A8/tmp/capture/capturedvideo.MOV"
2
推荐答案
重组你的代码。将读取器
初始化和 onloadend
中的回调放在 resolveLocalFileSystemURL
回调。
Reorganize your code a bit. Put the reader
initialisation and onloadend
callback inside the resolveLocalFileSystemURL
success callback.
这样:
var elements = new Object();
elements["video"] = $("#window_incident_create > .video > source")[0];
navigator.device.capture.captureVideo(
function(files) {
for ( var i in files ) {
var file = files[i];
var name = file.name;
var path = file.fullPath;
if ( path.indexOf("/private") === 0 )
path = "file://" + path.substr(8);
else
path = "file://" + path;
var type = file.type;
var lastModifiedDate = file.lastModifiedDate;
var size = file.size;
window.resolveLocalFileSystemURL(
path,
function(entry) {
console.log(1, entry.nativeURL);
var reader = new FileReader();
reader.onloadend = function(event) {
console.log(3);
elements["video"].type = type;
elements["video"].src = "data:" + type + ";base64," + event.target.result;
console.log(4);
};
reader.readAsDataURL(entry);
console.log(2);
},
function(error) {
console.log("0-0", error);
}
);
}
},
function(error) {
console.log("0-1", error);
},
{
limit: 1
}
);
这篇关于PhoneGap FileReader / readAsDataURL不触发回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!