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.
Related
We have to build an application for iOS and Android using PhoneGap Build and Apache Cordova.
The Phonegap version is 3.5.0. and we want to update the application when an internet connection is available. So we need to download some images files from online server to local file system into the application(iOS and Android).
Here is the example JavaScript code used:
try{
//The directory to store data
var store;
//Used for status updates
var $status;
//URL of our asset
var assetURL = "https://raw.githubusercontent.com/cfjedimaster/Cordova-Examples/master/readme.md";
//File name of our important data file we didn't ship with the app
var fileName = "mydatafile.txt";
//////////////////////
alert("Checking for data file.");
//Check for the file.
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, onError);
/////////////////////////////
} catch(e){
alert(e.message);
}
function downloadAsset() {
var fileTransfer = new FileTransfer();
alert("About to start transfer");
fileTransfer.download(assetURL, store + fileName,
function(entry) {
alert("Success!");
appStart();
},
function(err) {
alert("Error!");
console.dir(err);
alert(err);
});
}
//I'm only called when the file exists or has been downloaded.
function appStart() {
// $status.innerHTML = "App ready!";
alert( "App ready!");
}
function onFileSystemSuccess() {
try{
store = cordova.file.dataDirectory;
//Check for the file.
window.resolveLocalFileSystemURL(store + fileName, appStart, downloadAsset);
} catch(e){
alert(e.message);
}
}
function onError(){
alert('error');
}
When launching the application, the result is 2 alerts:
Cannot read property 'dataDirectory' of undefined // alert(e.message);
Error // alert('Error');
Ok, we found the problem!
Inside the app, we have included the cordova js file using the downloaded version of cordova plugin, which included in the js folder of the app.
So instead of the below:
<script type="text/javascript" src="js/cordova.js"></script>
it's recommended to type:
<script type="text/javascript" src="cordova.js"></script>
And Phongap will add the cordova file itself.
It may sound stupid but for all sort of "cannot read property" issues, you may also try
rm -rf plugins
cordova/ionic platform remove xxx
cordova/ionic platform add xxx
It solved my problem which doesn't really make sense at all. Who would expect the plugins themselves are getting wrong?
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 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);
}
);
}
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);
}