Write a file in phoneGap - android

I want to create file using the PhoneGap API File Writer, but using data my file will create in root folder either memory card or phone memory, but actually I want to create that file. Where does my installed app go? How do I know that where my files are installed and how do I create a file at that location?
Here is what I have:
function onDeviceReady() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
function gotFS(fileSystem) {
fileSystem.root.getFile("readme.txt", {create: true, exclusive: false}, gotFileEntry, fail);
}

See this last comment. So you can try to get /data/yourApp folder via fileSystem.root.getDirectory() etc. if it exists - so you are on sd card. go to the /data/yourApp and write there. If it doesn't exists - the root is already you app folder - just write the file.

Related

How to write file in root level directory rather than app data directory in Cordova android app?

I am building a Cordova android application and use cordova-plugin-file to write file on local system. The code I am using is as below:
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fs) {
console.log('file system open: ' + fs.name);
fs.root.getFile("newPersistentFile.txt", { create: true, exclusive: false }, function (fileEntry) {
console.log("fileEntry is file?" + fileEntry.isFile.toString());
writeFile(fileEntry, null);
}, onErrorCreateFile);
}, onErrorLoadFs);
The file newPersistentFile.txt will be created inside app data directory. How can I write file into system root level directory such as /Downloads, Notifications, etc.?
You want to look at the cordova.file directories (see docs).
So for downloads:
window.requestFileSystem(cordova.file.externalRootDirectory + "Downloads", 0, function(fs) {
console.log('file system open: ' + fs.name);
...
}

Download a file to Downloads folder of device using Cordova FileTransfer

I am using Cordova FileTransfer object to download a file from a url to device.
var fileTransfer = new FileTransfer();
var path = cordova.file.dataDirectory;
fileTransfer.download(
fileUrl,
path + "/sample.pdf",
function(theFile) {
console.log("download complete: " + theFile.toURI());
alert("File downloaded to "+cordova.file.dataDirectory);
},
function(error) {
console.log(JSON.stringify(error));
}
);
In this case the file is downloaded to data/data/com.fileDemo/files/
(I am not sure whether download is success as I can't access this folder. Getting success message as download complete: file:///data/data/com.fileDemo/files/sample.pdf).
How can I use the same method to download a file to "Downloads" folder of the android device?
In Cordova, with FileTransfer, you can request TEMPORARY or PERSISTENT file system
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, gotFS, fail);
iOS
PERSISTENT will return the Documents directory,
TEMPORARY will return the Caches directory
Android
PERSISTENT will returns the root of the SD card/phone memory
TEMPORARY will return a folder inside the data folder.
Refer File API & FileTransfer for more info.

Can't access root folder on android emulator with cordova

I'm trying crawl the sdcard of the phone using cordova 3.4. Currently I'am trying this on an android emulator.
I am able to acces the sdcard but the root folder is my application folder.
How can I access the root folder of the sdcard (/storage/sdcard) ?
Here is the code I'am using :
function getFileSystem(){
var deferred = $q.defer();
if(window.requestFileSystem){
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
function(fileSystem){
root = fileSystem.root;
console.log("Root file system : "+root.fullPath); // here I'm getting '/'
listFiles(root).then(function(files){
deferred.resolve(files);
}, function(error){
deferred.reject(error);
});
}, function(evt){ // error get file system
console.log("File System Error: "+evt.target.error.code);
}
);
}
return deferred.promise;
}
I have tried root.getParent() but it return undefined
Any idea ?

How to create a protected directory with Phonegap

I am developing a mobile application using Phonegap (Apache Cordova) ver. 3.3.0 for both Android platform and iOS.
This application should be able to create a directory in the mobile device, and access to this to save some files that I have downloaded from external urls.
The application, now, creates correctly the dir, and can access to it to save files.
The problem is that I want to make it a "protected" directory: the directort should be opened only pressing a button in my app, but inaccessible from anywhere else.
How can I realize this?
This is my code, for the creation of the directory.
document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap is ready
//
function onDeviceReady() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, fail);
}
function onFileSystemSuccess(fileSystem) {
console.log(fileSystem.name);
console.log(fileSystem.root.name);
var directoryEntry = fileSystem.root;
directoryEntry.getDirectory("myDir", {create: true, exclusive: false}, onDirectorySuccess, onDirectoryFail);
}
function onDirectorySuccess(parent) {
console.log(parent);
//parent.fullPath = path to dir in smartphone
window.localStorage.setItem("directory_path",parent.fullPath);
}
function onDirectoryFail(error) {
alert("Unable to create new directory: " + error.code);
}
function fail(evt) {
console.log(evt.target.error.code);
}
Your idea is impossible in cordova or any other language!
If you wish to accomplish something like that- use sqlite to create folder structure ant save data files there.

Phonegap/Cordova file API. Remove files on uninstall

I'm building a Phonegap/Cordova app that downloads some files and saves them on the device. For this I use the File API.
window.requestFileSystem(LocalFileSystem.PERSISTENT,
0,
function (fileSystem) {
rootPath = fileSystem.root.fullPath;
},
fail
);
On iOS this will set rootPath to the private directory of the app, which is good. On Android this will set rootPath to the root of the external storage, which is a bit of a problem since these files are not tied to the application and not removed when the App is deleted. As I understand it, the proper way of doing this on Android would be to use getExternalFilesDir. How can I get the functionality of getExternalFilesDir through Phonegap?
You'd want to request the external files directory via JS.
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
function (fileSystem) {
fileSystem.root.getDirectory("Android/data/com.my.app/files",
{create: true, exclusive: false},
function(dirEntry) {
rootPath = dirEntry.fullPath;
}, fail);;
},
fail
);
Now you have a path that points to an area that will be cleaned up when the app is uninstalled.
you cannot track uninstall event. but to delete directory the code is given below.
function deleteDirectory(directoyName){
$scope.fs.root.getDirectory(directoyName,{ create: false, exclusive: false }, function(dirEntry) {
dirEntry.removeRecursively(function() {
console.log('Directory Successfully Removed.');
}, errorHandler);
}, errorHandler);
}

Categories

Resources