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);
}
Related
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);
...
}
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 ?
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.
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.
At first:
YES, there are many solutions in StackOverflow, but non of them works in my case.
I got application built in SmartGWT.mobile
I attached config files and all needed files to this build to prepare it for PhoneGap
Application is build via build.phonegap.com site.
It works perfectly fine on Android 4.1.1
I want to:
Download file to local filesystem it is an PDF file - It is working fine using:
var fileTransfer = new FileTransfer();
fileTransfer.download(.....
Open PDF in native app (for eg. Adobe Reader) whichever is installed on android for PDFs - it is not working:
I tried:
(1)
cordova.exec("ChildBrowserCommand.showWebPage", encodeURI(theFile.toURL()) );
(2)
window.plugins.childBrowser.showWebPage(encodeURI(theFile.toURL()));
(3)
window.open(encodeURI(theFile.toURL()), '_blank', 'location=yes');
(4)
even HTML5 plugin for open PDFs by firefox
All variations with "file://" without with "./" at front and so on.
childBrowser shows only white screen, each time adds "http://" at front, window.open - the same.
I finally found something interesting like WebIntent, so i did:
window.plugins.webintent.startActivity({
action: window.plugins.webintent.ACTION_VIEW,
type: "application/pdf",
url: encodeURI(theFile.toURL().substring(7))},
function() {},
function() {alert('Failed to open URL via Android Intent')}
);
but its not working due to fact that phonegap-build not attaching class file and It can not find WebIntent Class
I declare this plugin using in config.xml:
<gap:plugin name="com.borismus.webintent.WebIntent" />
Do you know why it is not working, or what I'm doing worng ?
Maybe you know other way to open file just like that in native app, it suppose to be simple
I just want my app to download and show (in native app) the PDF for user.
don's FileOpener version have worked on my app cordova 3.0
phonegap local plugin add https://github.com/don/FileOpener
all the xmls, plugin, etc are then added automatically.
added fileopener.js on index.html
and then
window.plugins.fileOpener.open( path );
$("#page").on('pageshow', function(event, ui) {
if(event.handled !== true)
{
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
event.handled = true;
}
return false;
});
function fail() {
console.log("failed to get filesystem");
}
function gotFS(fileSystem) {
console.log("got filesystem");
// save the file system for later access
console.log(fileSystem.root.fullPath);
window.rootFS = fileSystem.root;
downloadImage(url, fileName);
}
function downloadImage(url, fileName){
var ft = new FileTransfer();
ft.download(
url,
window.rootFS.fullPath + "/" + fileName,
function(entry) {
console.log("download complete: " + entry.fullPath);
},
function(error) {
console.log("download error" + error.code);
}
);
}