Cordova iOS File API - Move Photo to a persistent storage - ios

I've a problem with moving a camera photo to the persistent storage under iOS 7 (Cordova 3.4.0-0.1.3 - File API 1.0.1).
I can capture the photo and when I move the file to the persistent storage it seems that there is no error, I also receive a file path with new_entry.fullPath like /my_folder/12345678.jpg.
But when I append the new image to the body with that url it seems that there is no image (blank image will be added). I've tried it also with "file://" in the url, but this makes no difference.
I'm also a little bit confused, because the new_entry.toURL() method returned an url containing a folder named "temporary" (e.g. cdvfile://localhost/temporary/my_folder/12345678.jpg), but I use the persistent storage. Is that correct under iOS?
This is my relevant code for that function:
var app = {
capturePhoto: function () {
if (!navigator.camera) {
alert('Camera API not supported');
}
navigator.camera.getPicture( app.cameraSuccess, app.cameraError, {
quality: 50,
destinationType: Camera.DestinationType.FILE_URI
});
},
cameraSuccess: function (imageData) {
console.log('cameraSuccess: '+imageData);
app.movePhoto( imageData );
},
movePhoto: function (file){
alert(file);
window.resolveLocalFileSystemURI( file , app.resolveOnSuccess, app.resOnError);
},
resolveOnSuccess: function (entry){
var d = new Date();
var n = d.getTime();
//new file name
var newFileName = n + ".jpg";
var myFolderApp = "my_folder";
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSys) {
fileSys.root.getDirectory( myFolderApp,
{create:true},
function(directory) {
entry.moveTo(directory, newFileName, function(new_entry){
path = new_entry.fullPath;
url = new_entry.toURL();
console.log(path+"\n"+url);
alert( path+"\n"+url );
jQuery('body').append('<img src="'+path+'" />');
}, app.resOnError);
},
app.resOnError);
},
app.resOnError);
},
resOnError: function(error) {
alert('Error '+error.code+': '+error.message);
},
}

27/5/2014 UPDATE: Version 1.1.0 was released since than, therefore no need to use dev branch anymore.
It's a bug in cordova: https://issues.apache.org/jira/browse/CB-6148
It's already fixed in dev branch. You can update to dev branch with those steps:
remove the plugin:
cordova plugin rm org.apache.cordova.file
install the plugin (we have to use the git syntax in this case):
cordova plugin add https://github.com/apache/cordova-plugin-file.git#dev
check the iOS build > Targets > Your app target > Build phases > Compile Sources
add (if not added)
CDVFile.m
CDVLocalFilesystem.m
CDVAssetLibraryFilesystem.m

Related

Cordova Media Plugin not working

I can't get the cordova media plugin to work, I get error code 1 which signals that the file is not getting loaded. I've tried a number of variations, but can't figure out which path is correct.
Currently my code looks like this:
function onDeviceMediaReady () {
var path = window.cordova.file.applicationDirectory + 'why.mp3';
console.log(path);
narrative = new Media(path, // success callback
function() {
console.log("playAudio():Audio Success");
},
// error callback
function(err) {
console.log("playAudio():Audio Error: "+ err.code);
});
}
this gives me a path that's file://var/cotainers/Bundle/Application/[GUID]/Cordova400.app/why.mp3
I don't get why I can't find it. the file is in the telerik appbuilder root directory.
Inside your 'WWW' folder place the file inside 'sound' folder & try following
var srcBookmark = "sound/yes.mp3"; //ios
var iOSPlayOptions = {
numberOfLoops: 1,
playAudioWhenScreenIsLocked : false
}
var media = $cordovaMedia.newMedia(srcBookmark);
//media.play(); //android
media.play(iOSPlayOptions); //ios
$timeout(function(){
media.stop();
media.release()
}, 500);
If you're using appBuilder to test your app te aplication folder is the telerik folder, you must deploy your app as APK and test the folder

Why is cordova-plugin-file giving me null for cordova.file.dataDirectory?

Running on an iPhone 6 through Xcode 7.3 and testing in Safari Web Inspector, I am using the Cordova File plugin. I am running example code copied right out of the cordova-plugin-camera docs for saving an image:
function createNewFileEntry(imgUri) {
console.log("saveImage->createNewFileEntry");
window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function(dirEntry) {
console.log("createNewFileEntry dir: " + dirEntry);
// name the file
var d = getDateTime();
var fileName = "cc" + d + ".jpeg";
// JPEG file
dirEntry.getFile(fileName, { create: true, exclusive: false }, function (fileEntry) {
console.log("createNewFileEntry file: " + fileEntry.fullPath);
writeFile(dstFileEntry, imgUri);
}, function() { console.log("Error: createNewFileEntry->getFile"); });
}, function() { console.log("Error: createNewFileEntry->resolveLocalFileSystemURL"); });
}
What happens is that window.resolveLocalFileSystemURL()fails because directory spec cordova.file.dataDirectory is null. In fact ALL possible cordova.file.* directories are null. Why?
The following graphic should answer relevant questions- cordova 4.1.1 for iOS is running, cordova File is present, window.resolveLocalFileSystemURL() is valid.
Why are all the symbolic Directory names null?
This is the config.xml file that is at the root of the app, ./config.xml
This config.xml is in the ios platform, ./platforms/ios/PayLive/config.xml

Contact list not accessing all contacts phonegap

I created a project in phonegap in appery.io framework. It is not cordova based project but I installed cordova library manually. I am fetching contacts list using Jquery but it is fetching only device contacts list not the sim contacts list. If i want to install cordova contacts plugin using commands then it is showing current directory is not a cordova based project. if anyone could help me out it would be much appreciated.
Hi use this to save all contacts in sdcard and display. (default Cordova contacts and file plugin)
document.addEventListener("deviceReady", deviceReady, false);
function deviceReady() {
navigator.contacts.find(["*"], function(contacts) {
//alert("contacts.length = " + contacts.length);
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {
fileSystem.root.getFile("contacts.json", {create: true, exclusive: false}, function(fileEntry) {
fileEntry.createWriter(function(writer) {
writer.onwriteend = function(){
// Success Contacts saved to sdcard as a contacts.json file
// Now get and read the json file
var path = fileSystem.root.getFile("contacts.json", {create:false},gotFileEntry, fail);
// jquery
$.getJSON(path, function (data) {
user = data;
$.each(user, function (index, user) {
var all_item = '<p id="'+user.id+'">'+user.displayName+'</p>';
$('#allcontacts').append(all_item);
});
});
};
writer.write(JSON.stringify(contacts));
}, onError);
}, onError);
}, onError);
}, onError,{"multiple": true});}
function onError(){
alert("Error");
}

How to download picture with Phonegap Filetransfer to image gallery in iOS

This has been asked several times, but most of these questions are unanswered
I'm downloading a file like below, and it seems to work fine. However .. it does not show up in the iOS, ehm, gallery. That is, in the 'photos' application.
var fileTransfer = new FileTransfer();
var encurl = encodeURI(url);
var filename = url.split('/').slice(-1)[0];
var filepath = "foo/"+filename;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
var syspath = fileSystem.root.toURL() + '/' + filepath;
var fileTransfer = new FileTransfer();
fileTransfer.download(
encurl,
syspath,
function (entry) {
foo.debug("Download success "+syspath);
},
function (error) {
foo.error("Download failed with error "+error.code+' '+syspath);
}
);
}, function (evt) {
foo.error("Filesystem failed with error "+evt.target.error.code);
});
and the result is
[Log] Download success file:///var/mobile/Applications/68DE0AD9-FBD2-4D82-92C0-2B7634B218D5/Documents//foo/20141030-153810-editor.jpg (console-via-logger.js, line 173)
hurray. now, how do you open the download, using just your fingers, on ios ?
I would mark this as a duplicate of Phonegap - Save image from url into device photo gallery
I was happy with the answer by M165437 and my comments. That answered my question.
Try to download by using https://www.npmjs.com/package/com-cordova-image-save-to-gallery plugin. It will download a picture from a given URL and save it to IOS Photo Gallery.
Cordova plugin add https://github.com/valwinjose007/cordova-image-save-to-gallery.git
How to use:
declare var CordovaImageSaveToGallery: any;
CordovaImageSaveToGallery.downloadFromUrl('https://picsum.photos/200/300',(res)=>{
//download success
},(err)=>{
//error on download
});

File Transfer Download with Cordova 3.4 for iOS 7

I'm a little confused with the filetransfer method of Cordova for iOS. (I had a version working with Android)
Apparently, I don't set the destination folder properly. target:null Could not create target file Note that I assume that the directory exists as it is created with success earlier in the script.
According to the Cordova documentation, I should use a entry.toURL to get the right path.
function download(filename){
var localPath = rootFS.toURL+'contentImages/'+filename;
var fileTransfer = new FileTransfer();
fileTransfer.download(encodeURI('http://myValidatedSource.com/'+filename),
localpath,
function(entry){
console.log('download completed for '+entry.fullPath);
},
function(error){
console.log(error);
}
);
}
I also tried this:
alert(rootFS.fullPath); ==> "/"
and
alert(rootFS.toURL); ==> "function(){
if (this.nativeURL){
return this.nativeURL;
}
return this.toInternalURL()|| "file://localhost"+this.fullPath";
}"
I was not so far...
var localPath = rootFS().toURL+'contentImages/'+filename;
instead of
var localPath = rootFS.toURL+'contentImages/'+filename;

Resources