I am making an app using Ionic framework. I have used ngcordova angular library.
I have a zip folder which we are unzipping and then reading / writing the file.
When I am updating (write operation) on any file in "non rooted" phones, we are getting NO_MODIFICATION_ALLOWED_ERR. But this works fine in a "rooted" phone.
This issue is with write operation only. It is working fine while reading any file.
Please have a look at the code.
UpdateConfiguration: function (appconfig) {
var defer = $q.defer();
$cordovaFile.checkFile(cordova.file.dataDirectory, "Stock/database/appconfig.json").then(function (success) {
$cordovaFile.writeFile(cordova.file.dataDirectory, 'Stock/database/appconfig.json', JSON.stringify(appconfig), true)
.then(function (success) {
defer.resolve(true);
}, function (err) {
alert(JSON.stringify(err));
defer.reject(false);
});
}, function (error) {
alert('Error finding app config file');
});
return defer.promise;
}
Please help.
Thanks in advance.
Related
i implemented the deploy service to my ionic app (i am using ionic 1) and it is working fine, now i want to show the users the time remaining for the download or maybe a progress bar so they do not think that the app is freezing.
below is the function of the deploy
var deployFunction = function() {
$ionicDeploy.check().then(function(snapshotAvailable){
if (snapshotAvailable) {
// When snapshotAvailable is true, you can apply the snapshot
MainService.startSpinner("Downloading Updates");//this shows a loading image indicating that the download started
//applying the snapshot
$ionicDeploy.download()
.then(
function() {
MainService.stopSpinner();
MainService.startSpinner("Extracting");
$ionicDeploy.extract()
.then(
function(){
MainService.stopSpinner();
$ionicDeploy.load();
}, function(error) {
console.log("ERROR EXTRACT "+error);
// Error extracting
}, function(progress) {
// progress of extracting
console.log('extraction progress '+progress);
}
);
}, function(error){
//download error
console.log("ERROR Downloading "+error);
}, function(progress) {
//download progress
console.log('download progress '+progress);
}
);
}
});
}
i've read somewhere that the progress function should return an integer...
but it is not and i have no idea how to get information about the download beside that it is started or it is finished.
any help would be appreciated
for future references this was solved based on this documentation, the code will be
$ionicDeploy.download({
onProgress: function(p) {
console.log(p);
}
})
.then(
function() {...
i tried it and the console logged numbers from 1 to 100 indicating the download progress.
So I am building a cordova/phonegap app in angularjs 1 and I'm trying to save and read from a file called calendar.txt in the app's private directory/sandbox and can't.
My console logs while debugging show that there are no errors and the file is being created if it doesn't exist, and is being read correctly. However that is not the case. When I build and run on my device, the data is not saved. Also no file is created in the location specified.
I console logged the path it was trying to use and this is it:
file:///data/data/com.adobe.phonegap.app/files/calendar.txt
Here is the code I am using to open the file:
$rootScope.openFile = function(){
var pathToFile = cordova.file.dataDirectory + "calendar.txt";
console.log('path = ' + pathToFile);
window.resolveLocalFileSystemURL(pathToFile,
function(fileEntry){
fileEntry.file(function (file) {
var reader = new FileReader();
reader.onloadend = function (e) {
$rootScope.calendar = JSON.parse(this.result);
console.log('file opened');
console.log(JSON.parse(this.result));
};
reader.readAsText(file);
}, function(error){});
}, function(error){
if(error.code == FileError.NOT_FOUND_ERR){
$rootScope.calendar = new Year();
console.log('no file found so it was created');
$rootScope.saveFile();
}
else{
console.log(error);
}
});
};
And here is the code for my save the file:
$rootScope.saveFile = function(){
var data = JSON.stringify($rootScope.calendar, null, '\t');
var fileName = "calendar.txt"
window.resolveLocalFileSystemURL(cordova.file.dataDirectory,
function(directoryEntry){
directoryEntry.getFile(fileName, { create: true },
function (fileEntry) {
fileEntry.createWriter(
function (fileWriter) {
var blob = new Blob([data], { type: 'text/plain' });
fileWriter.write(blob);
console.log('file saved');
},
function (error){});
},
function (error){}
);
},
function(error){
console.log("Saving Error: Error during finding directory", error.message);
}
);
};
I have used this tutorial to get this far: Cordova File Plugin Tutorial
What am I doing wrong?
I assume that you are facing this issue in Android as i too faced the same. As per my understanding and googling, in android (atleast in android 5) you cant write in application data directory unless the phone is rooted. So you may have to use externalRootDirectory instead.
eg: window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory,successCallback, errorCallback);
Hope it helps.
I am trying to figure out the type of the success object for any of the cordova file API functions? For instance, for the checkFile function it should logically be a boolean true / false reply.
Or is the then branch the positive - file exists branch and the error branch when the file does not exist?
$cordovaFile.checkFile(cordova.file.dataDirectory, "some_file.txt")
.then(function (success) {
// success
}, function (error) {
// error
});
However, it's pretty hard to tell from the documentation.
Docs states: Returns Object , and since it seems difficult to detect from a standard browser, it's some dark magic phone debugging to get familiar with the API. Any hints welcome.
After some exercises and looking at the source code, this is how I now got to understand the cordova file library with the checkFile function:
this.checkBackupExists = function(){
var deferred = $q.defer();
$cordovaFile.checkFile(cordova.file.dataDirectory, this.backupFileName)
.then(function (success) {
deferred.resolve(true);
}, function (error) {
// error
if(error.code==1) #NOT_FOUND_ERR
deferred.resolve(false);
else
deferred.reject(error);
});
return deferred.promise;
}
I believe that a successful run of 'checkFile' with result in the 'then' callback being called and and of the errors occurring here http://ngcordova.com/docs/plugins/file/#file-error-codes will result in the 'error' callback being called... with 'error' being that error code or an object containing the code.
I have asked various questions and got only limited answers and it has gotten me this far:
Mongoose code :
app.get('/Download/:file(*)', function (req, res) {
grid.mongo = mongoose.mongo;
var gfs = grid(conn.db);
var file = req.params.file
var fs_write_stream = fs.createWriteStream('Program.cs');
var readstream = gfs.createReadStream({ filename: file });
readstream.pipe(fs_write_stream);
res.download(__dirname+'/Program.cs', 'Solution.cs', function (err) {
if (err) throw err;
else console.log("check console");
});
})
Angular code:
$scope.Download = function () {
$http.get(url + "/Download/"+"Program.cs")
.success(function (res) {
console.log(res);
})}
When I open my console angular just returns a blank line, when i look into the network aspect the header sais this:
I want to be able to store this file to my local file system to a path that I want to be able to choose myself. PLEASE help me out, I'm desperate, I have looked EVERYwhere for a solution, thank you very much in advance!
Try this instead of your client side download attempt:
<a class="btn" href="/Download/Program.cs" download>
What is the way to save a canvas to your phone album (camera roll, etc.) using Phonegap?
And when saved, how do I get the image URL using Cordova? Which plugins should I consider for this?
Method 1: Canvas2Image Plugin
With this plugin, you can save a canvas to your phone library using:
<canvas id="myCanvas" width="165px" height="145px"></canvas>
function onDeviceReady()
{
window.canvas2ImagePlugin.saveImageDataToLibrary(
function(msg){
console.log(msg);
},
function(err){
console.log(err);
},
document.getElementById('myCanvas')
);
}
However, it is not clear how the imageURI is passed or returned after saving?
UPDATE 23/02. I tried the following, but it never reaches my successcallback:
$scope.done = function() {
// todo: iterate over cloaks
$ionicLoading.show({
template: 'Saving images...'
});
// #issue: callback is not called
saveCanvasToPhone().then(successCallback, errorCallback);
var successCallback = function(output) {
$ionicLoading.hide();
window.alert("success call back: " + output) // never reached
$scope.addNewImage(output);
$state.go('tab.dash', {}, {reload: true}); // does not work
$state.go('tab.dash') // does not work
$scope.refreshLocalstorage();
}
// #issue4-nl: not working
var errorCallback = function(error) {
$ionicLoading.hide();
console.log("error cb: "+ error)
window.alert("error cb: " + error)
}
function saveCanvasToPhone() {
var defer = $q.defer();
try {
window.canvas2ImagePlugin.saveImageDataToLibrary(
function(output){
$scope.addNewImage(output); // does not work to save imagepath on localstorage
$scope.debugEntryOther = output // works fine
defer.resolve(output)
$ionicLoading.hide()
},
function(error){
defer.reject(error)
$ionicLoading.hide()
window.alert(error)
},
document.getElementById('canvas')
);
} catch(error) {
defer.reject(error)
$ionicLoading.hide()
console.log("saveCanvasToPhone: trycatch: error: " + error)
}
return defer.promise;
} // save canvas to phone
} // done
Unfortunatly Canvas2Image plugin was originally designed to save the canvas to the gallery, not to save to a file and deal with the file later on.
I have not found alternatives to this plugin (nor have I really searched), but you can manage to get the file path where the file was saved.
It depends on wich platform you are targeting :
For android, the path to the file is passed in the parameter 'msg' of the success function
For Windows Phone, the 'msg' parameter returns the string 'Image saved :' followed with the path of the file. So you have to split the string.
For IOS, it is actually not possible with the original plugin, but there are several forks allowing to do it.
I was informed that the author will soon take some time to improve his plugin so I'm sure it will soon be possible to get the file path with the 'official' plugin even for IOS.
In the meantime, you could use my fork. In that case, for IOS the path is returned in the 'msg' parameter, just like for android.