Cordova 3.6.3 File Plugin - get local video file on android
What I would like to do is
- get the URI of a video file on a device via the cordovas javascript API
- set URI as attribute value
video
video
for HTML5video
.
The second part shouldn't be a problem.
Regarding the first task, there are many good structured tutorials, such as Raymond Camden , demonstrating how to get local files via javascript in cordova .
However, with the newest version of cordova, I couldn't get it to work.
Video file
The video is either in assets/www/videos/testvid.webm
or res/raw/testvid.webm
in the embedded apk file. Both options don't work.
javascript
myPath = cordova.file.applicationDirectory; // -> file:///android_asset/
//myPath += "www/videos/testvid.webm";
respectively
myPath = cordova.file.applicationStorageDirectory; // -> file:///data/data/com.example.MyPackage/
//myPath += "raw/testvid.webm";
Then:
window.resolveLocalFileSystemURL(myPath, gotFile, fail);
function gotFile(entry){
if(entry.isDirectory)
alert JSON.stringify(entry.getFile("testvid.webm"));
}
Permissions
The res/xml/config.xml
added access permission
<preference name="AndroidExtraFilesystems" value="files,files-external,documents,sdcard,cache,cache-external,root" />
Error: {code: 1} → NOT_FOUND_ERR
What am I doing wrong? How do I navigate to the file or where can I find it?
source to share
I understood that!
There is a file in the android version of the cord file .
A workaround is to move the file (s) from the resource directory of the application itself file:///android_asset/
(cordova.file.applicationDirectory) to the working directory on the phone, for example file:///data/data/com.example.MyPackage/files
(cordova.file.dataDirectory). Then set the video source URL to this new file.
XMLHttpRequest as well as FileTransfer will do the trick.
var myFilename = "testvid.webm";
var myUrl = cordova.file.applicationDirectory + "www/videos/" + myFilename;
var fileTransfer = new FileTransfer();
var filePath = cordova.file.dataDirectory + myFilename;
fileTransfer.download(encodeURI(myUrl), filePath, (function(entry) {
/*
res = "download complete:\n"
res += "fullPath: " + entry.fullPath + "\n"
res += "localURL: " + entry.localURL + "\n"
alert(res += "nativeURL: " + entry.nativeURL + "\n")
*/
var vid = document.getElementById("someID");
vid.src = entry.nativeURL;
vid.loop = true;
}), (function(error) {
alert("Video download error: source " + error.source);
alert("Video download error: target " + error.target);
}), true, {
headers: {
Authorization: "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
}
});
source to share