I'm using Cordova 4.2.0 for Android.
I have some troubles to get FileTransfer plugin work properly. I suppose that there is a writing error
exception:".myApp\/contentImages\/20150110220101.jpg: open failed: ENOENT (No such file or directory)"
filename was previously tested and does not exist yet:
rootFS.getFile('.myApp/contentImages/'+file,{create:false},
function(){
console.log(file+' already exists');
},
function(error){
console.log(file+" does not exist locally");
console.log("Error #"+error.code);
download(file);
}
);
And here is the download function:
function download (filename){
var localPath = rootFS.fullPath+'/.myApp/contentImages/'+filename;
var fileTransfer = new FileTransfer();
fileTransfer.download(
encodeURI('http://distantApp/contentImages/'+filename), // This file exists
localPath,
function(entry) {
console.log("download complete: " + entry.fullPath);
},
function (error) {
console.log('download error: ' + error.code + ": "+error.exception+" ; source " + error.source+" ; target " + error.target);
}
);
}
What could be the problem?
EDIT
Code for rootFS
function onDeviceReady(){
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, function(){
console.log("error requesting LocalFileSystem");
});
}
function gotFS(fileSystem) {
console.log("got filesystem: "+fileSystem.name); // displays "persistent"
console.log(fileSystem.root.fullPath); // displays "/"
window.rootFS = fileSystem.root;
}
The problem was caused by an upgrade of Cordova from a previous version.
The path of local files was not properly identified: .fullPathis now obsolete and should be replaced by .toURL().
I think your problem is not with FileTransfer plugin, but the way you are trying to check if the file exists.
Looking here: http://www.html5rocks.com/en/tutorials/file/filesystem/ you we'll see that accessing to a file which its immediately parent does not exist raise an exception:
Inside the callback, we can call fs.root.getFile() with the name of the file to create. You can pass an absolute or relative path, but it must be valid. For instance, it is an error to attempt to create a file whose immediate parent does not exist.
I am wondering if the problem is that the parents of your file don't exist. In this case the folders .myapp and contentImages.
Related
I am working on an Ionic project for uploading videos/images.
Every time on the initial run, it uploads without a hitch. But, on the second file upload and everyone thereafter - I receive this error:
ERROR:
{"code":3,"source":"file:///storage/emulated/0/Android/data/com.ionicframework.videocap/files/videolocal.mp4","target":"/upload.php","http_status":null,"body":null,"exception":"state:
2"}
The code is as such,
var options = new FileUploadOptions();
options.quality=10;
options.fileKey="file";
options.fileName="videodame.mp4";
options.chunkedMode=false;
options.mimeType="video/mp4";
options.trustAllHosts=true;
var headers={'Connection':'close'};
options.headers = headers;
$cordovaFileTransfer.upload("http://76.181.104.114/Dame/upload.php", videomp4, options).then(function(result) {
console.log("SUCCESS: " + JSON.stringify(result.response));
console.log("SCOPECLIPmp4: " + $scope.clip + '.mp4');
}, function(err) {
console.log("ERROR: " + JSON.stringify(err));
}, function (progress) {
//console.log("PROG:" + progress);
});
Any help would be greatly appreciated. Please ask if extra info is required! Thanks!
Found my solution, had a duplicate variable that was causing the second video to reference variables from the first video.
I'm a genius, it's whatever.
What I'd like to do is
get the URI of a video file on the device via cordovas javascript API
set the URI as value of a HTML5 video tag's src attribute.
The second part shouldn’t be a problem.
Concerning the first task, there are a lot of good structured tutorials like Raymond Camden's demonstrating how to get local files through javascript in an cordova environment.
However, with the newest version of cordova, I could not get it to work.
The video file
The video is located either in assets/www/videos/testvid.webm or res/raw/testvid.webm in the built apk file. Both variations did not work.
The 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"));
}
The permissions
In res/xml/config.xml access permissions are added
<preference name="AndroidExtraFilesystems" value="files,files-external,documents,sdcard,cache,cache-external,root" />
The error is {code:1} -> NOT_FOUND_ERR
What am I doing wrong? How to navigate to the file, or where can one put it to be found?
I figured it out!
There is a bug in the android version of the cordova file plugin.
A workaround is transferring the file(s) from the assets directory of the app itself file:///android_asset/ (cordova.file.applicationDirectory) to a working directory on the phone like file:///data/data/com.example.MyPackage/files (cordova.file.dataDirectory). Then set the video's 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=="
}
});
I saw several posts related to this like Ref but not working for me . I tried like below
First method:
Directly set image source from contact image value
var myImg = document.getElementById("cimg");
myImg.src = contacts[i].photos[0].value;
this one not display the image in img tag.
Second Method:
Pass the contact image value to the fileDownload option
window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, function(fs){
fs.root.getFile("temp.jpg", {create: true, exclusive: false},
function(entry){
//alert(entry.toURL());
var fileTransfer = new FileTransfer();
fileTransfer.download(
contacts[i].photos[0].value, // the filesystem uri you mentioned
entry.fullPath,
function(entry) {
// do what you want with the entry here
console.log("download complete: " + entry.fullPath);
var src = entry.fullPath;
//$("").append('<img src="'+src+'" >');
},
function(error) {
alert("error source " + error.source);
console.log("error target " + error.target);
console.log("error code " + error.code);
console.log(error);
},
false,
null
);
}, function(e){
console.log("file create error",e);
});
}, null);
This one shows error like 07-28 07:37:56.468: E/FileTransfer(20986): {"target":"file:\/\/\/mnt\/sdcard\/Android\/data\/io.cordova.hellocordova\/cache\/temp.jpg","http_status":0,"code":3,"source":"content:\/\/com.android.contacts\/contacts\/2\/photo","exception":"read failed: EINVAL (Invalid argument)"}
07-28 07:37:56.468: E/FileTransfer(20986): java.io.IOException: read failed: EINVAL (Invalid argument)
Edit :
my phonegap contact[i].photo[0].value return like "content://com.android.contacts/contacts/1/photo"
how to resolve this one. Please help me.
please check what android version you are testing. Because there have a lot changes structures android 2.0 -> 2.2.. Can you try on android above 4.0 + ?
I am trying to download a PDF in my phonegap application and then immediately open it, but I get a File not Found error. It seems to download. Here is my code:
// Download
var url = "https://example/example.pdf";
var fileTransfer = new FileTransfer();
var uri = encodeURI(url);
var fileURL = 'cdvfile://localhost/persistent/com.mycompany.myApp/';
var fileName = 'example.pdf';
fileTransfer.download(
uri,
fileURL,
function (entry) {
alert("download complete: " + entry.fullPath);
// Open
cordova.plugins.fileOpener2.open(
fileURL + fileName,
'application/pdf',
{
error: function (errorObj) {
alert('Error status: ' + errorObj.status + ' - Error message: ' + errorObj.message + ' ' + errorObj.fileURL);
},
success: function () {
alert('file opened successfully');
}
}
);
},
function (error) {
alert("download error source " + error.source);
alert("download error target " + error.target);
alert("upload error code" + error.code);
}
);
alert('Downloading...');
I'm also wondering where is the best place to save files like this, that should be available after the app is closed?
second argument to fileTransfer.download is the location on device where you want to download pdf file.
You are setting var fileURL = 'cdvfile://localhost/persistent/com.mycompany.myApp/'; Which I believe does not exist. You can use file system plugin to first get file location on your device and then save it on device. you can use below code snapet.
var url = "https://example/example.pdf";
var uri = encodeURI(url);
//var fileURL = 'cdvfile://localhost/persistent/com.mycompany.myApp/';
// var fileName = 'example.pdf';
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
function gotFS(fileSystem) {
fileSystem.root.getDirectory("Dir_NAME", {
create: true,
exclusive: false
}, gotDirectory, fail);
}
function gotDirectory(entry) {
entry.getFile('yourFileNAme', {
create: true,
exclusive: false
}, gotFileEntry, fail);
}
function gotFileEntry(fileEntry) {
filePath = fileEntry.fullPath;
var fileTransfer = new FileTransfer();
fileTransfer.download(
'uri', filePath, function(entry) {
console.log("success");
//perform you launch task
}, function(error) {
console.log("error");
}, true, {});
}
I hope it will help you.
Even I am new to android but what I think that there is problem in URI
To open up a Uri in android it should have a structure like
file:///storage/....
(there should be 3 backslashes )
You can get more details here
The best place to save a pdf can be your sdcard or internal storage. Then the pdf will be available in your device even after the application closes.
If you wish to have the same pdf file, available in different devices while testing your application in different device/ or even in emulator you can save it in the assert folder of your android project.
hope this helps
I am not sure why but when I do the following
<img id="buttons" src="https://qikout.com/apppics/MerchantPromo.png" style="margin:1% auto; width:99% !important;" onclick="openextlink('https://qikout.com');"/>
function openextlink(url)
{
var ref = window.open(url, '_blank', 'location=no');
//window.plugins.childBrowser.showWebPage(url, { showLocationBar: true });
}
it works fine. But when I want to run the openextlink from within another function it does not open the window, instead it loads the page but just does not show it to the user.
for e.g.
function scan(){
window.plugins.barcodeScanner.scan( function(result) {
if(result.format != "QR_CODE")
{
alert("That is not a valid google Tag");
}
else
{
n = result.text.split(".com/");
loadpet();
//alert("We got a barcode\n" +
// "Result: " + result.text + "\n" +
// "Format: " + result.format + "\n" +
// "Cancelled: " + result.cancelled);
}
}, function(error) {
alert("Scanning failed: " + error);
}
);
}
function loadpet(){
$("#buttons").trigger("click");
var link = "http://www.google.com/" + n[1];
openextlink(link);
}
it works via
<div onclick="scan();" class="menu-right"></div>
Basically it scans fine, it gets the result it moves the result to the loadpet function and even sends it to the openextlink function however does not popup the viewer.
Your window.open code looks right (based on how InAppBrowser works as of Cordova 3.0).
Can you confirm all of the following?
The InAppBrowser plugin has an entry in your /#myapp#/platforms/#platform#/www/cordova_plugins.js file?
The InAppBrowser plugin folder is in /#myapp#/platforms/#platform#/plugins/
The related SRC files for InAppBrowser are in the platform folder as well (for example IOS plugin src files are: /#myapp#/platforms/#platform#/#appname#/Plugins/)
Also if all the above are confirmed but still no luck you might try appending a console.log('inappbrowser.js file has loaded!') line to the end of your inappbrowser.js file to confirm it is indeed loading (assuming you have setup the debug console and can view the output).