Why this works :
myFile1 = "myReadme.txt";
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
function gotFS(fileSystem) {
fileSystem.root.getFile(myFile1, {create: true, exclusive: false}, gotFileEntry, fail);
}
function gotFileEntry(fileEntry) {
alert("good");
}
function fail(error) {
alert("Error");
}
and that doesn't work ?
myFile2 = cordova.file.externalDataDirectory + "myReadme.txt";
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
function gotFS(fileSystem) {
fileSystem.root.getFile(myFile2, {create: true, exclusive: false}, gotFileEntry, fail);
}
function gotFileEntry(fileEntry) {
alert("good");
}
function fail(error) {
alert("Error");
}
Can we not use cordova.file.dataDirectory or cordova.file.externalRootDirectory or externalDataDirectory for Android ?
Thank you
I'm gonna try to answer with this issue (but I'm sure we can to do better ? )
First, when I plug my device to my computer the tree path is about like that :
MyFile1.txt
MyApplication
MyFiles
MyCache
Application
DCIM
media
Movies
Musics
Android
data
data (I think is not accessible when your are not rooted ???)
com.MyDomainName.MyAppName
cache
files
Then,
Following this official Cordova document : http://plugins.cordova.io/#/package/org.apache.cordova.file
, My first wish is to store my files and datas with "cordova.file.dataDirectory" or "cordova.file.externalDataDirectory" or another else ... ?
when you're doing console.log(cordova.file.dataDirectory) you obtain file:///data/data/com.MyDomainName.MyAppName/files/ => This, you cannot access when you are no rooted
and when you're doing console.log(cordova.file.externalDataDirectory) you obtain
file:///storage/emulated/0/
and when you're doing that
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
function gotFS(fileSystem) { console.log(fileSystem.root) ...
you can see in this object : nativeURL: "file:///storage/emulated/0/" ... so, it's the same like "externalDataDirectory" and I suppose we cannot use "dataDirectory" ...
Anyway, I don't know whether it's the best practice but here you are my solution :
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFSWin, getDirectoryFail);
function onFSWin(fileSystem) {
fileSystem.root.getDirectory("myFolder", {create: true, exclusive: false}, getDirectorySuccess, getDirectoryFail);
}
function getDirectorySuccess(parent) {
console.log("New Folder or Folder already exists: " + parent.fullPath);
}
function getDirectoryFail(error) {
console.log("Unable to create new directory: " + error.code);
}
// Second, we gonna check or create a new file in a specific folder
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFSWin2, onFSFail2);
function onFSWin2(fileSystem) {
fileSystem.root.getFile("myFolder/myFile1.txt", {create: true, exclusive: false}, gotFileEntry, onFSFail2);
}
function gotFileEntry(fileEntry) {
console.log("New file: " + fileEntry.fullPath);
fileEntry.createWriter(gotFileWriter, onFSFail2);
}
function gotFileWriter(writer) {
writer.onwriteend = function(evt) {
console.log("contents of file now 'some sample text'");
writer.truncate(11);
writer.onwriteend = function(evt) {
console.log("contents of file now 'some sample'");
writer.seek(4);
writer.write(" different text");
writer.onwriteend = function(evt){
console.log("contents of file now 'some different text'");
}
};
};
writer.write("some sample text");
}
function onFSFail2(error) {
console.log(error.code);
}
You can use resolveLocalFileSystemURL to use the cordova.file.* properties:
window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function (dirEntry) {
dirEntry.getFile("myFile1.txt", {
create: true,
exclusive: false
}, function (fileEntry) {
// fileEntry.createWriter
}, onError);
});
Related
I'm trying to get external storage path in Phonegap. I'm using this code but it never handles the correct SDCard path.
(function(){
window.appRootDirName = ".myapp";
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
alert(cordova.file.externalRootDirectory);
console.log("device is ready");
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
function fail() {
console.log("failed to get filesystem");
}
function gotFS(fileSystem) {
console.log("filesystem got");
fileSystem.root.getDirectory(window.appRootDirName, {
create : true,
exclusive : false
}, dirReady, fail);
}
function dirReady(entry) {
window.appRootDir = entry;
alert(JSON.stringify(window.appRootDir));
}
})();
also
<preference name="AndroidPersistentFileLocation" value="Internal" />
and
<preference name="AndroidPersistentFileLocation" value="Emulated" />
doesn't help.
Are there a way to get the correct SDCard path on Android 6 with phonegap?
This is how i made it work in android as well as in iOS,
function writeFile() {
if (sessionStorage.platform.toLowerCase() == "android") {
window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory, onFileSystemSuccess, onError);
} else {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, onError);
}
}
function onError(e) {
alert("onError");
};
function onFileSystemSuccess(fileSystem) {
var entry = "";
if (sessionStorage.platform.toLowerCase() == "android") {
entry = fileSystem;
} else {
entry = fileSystem.root;
}
entry.getDirectory("Folder_Name", {
create: true,
exclusive: false
}, onGetDirectorySuccess, onGetDirectoryFail);
};
function onGetDirectorySuccess(dir) {
dir.getFile(filename, {
create: true,
exclusive: false
}, gotFileEntry, errorHandler);
};
function gotFileEntry(fileEntry) {
// logic to write file in respective directory
};
function errorHandler(e) {
// handle error
}
Try out this one.
document.addEventListener("deviceready", onDeviceReady, false);
// device APIs are available
//
function onDeviceReady() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, fail);
}
function onFileSystemSuccess(fileSystem) {
fileSystem.root.getDirectory("App_files", {create: false, exclusive: false}, onGetDirectoryWin, onGetDirectoryFail);
}
function fail(evt) {
console.log(evt.target.error.code);
}
var onGetDirectoryWin = function(parent) {
}
var onGetDirectoryFail = function() {
console.log("error getting dir")
}
I am trying to rename file using Cordova File Plugin. It gives me error with Code 1000 without any description. Here is code sample I am using
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
function gotFS(fileSystem) {
console.log('root url '+fileSystem.root.toURL());
var entry = new FileEntry("Download/abc.pdf");
fileSystem.root.getDirectory("Download", {create: true, exclusive: false},
function (directory) {
entry.moveTo(directory, "file.pdf", success, fail);
}, fail);
}
function success(fileEntry) {
console.log("New Path: " + fileEntry.fullPath);
}
function fail(error) {
console.log("Error: " + error);
}
I have already placed abc.pdf in Download folder.
Not sure what I am doing wrong.
I am using Cordova 4.0.0 with Android (platform version 3.4.0)
It worked but with following way,
fileSystem.root.getFile("Download/abc.pdf", {}, function(file){
fileSystem.root.getDirectory("Download", {}, function (directory) {
file.moveTo(directory, "file.pdf", success, fail);
}, function(error){
console.log(error,"Directory Error ");
});
}, function(error){
console.log(error,"File Error ");
});
I got a dirty (?) working version of same as
var entry = new FileEntry("abc.pdf");
entry.fullPath = "//Download/abc.pdf";
entry.nativeURL = fileSystem.root.toURL() + "Download/abc.pdf";
entry.filesystem = new FileSystem('persistent');
var dirEntry = new DirectoryEntry("Download");
dirEntry.fullPath = "//Download/";
dirEntry.nativeURL = fileSystem.root.toURL() + "Download/";
dirEntry.filesystem = new FileSystem('persistent');
entry.moveTo(dirEntry, "file.pdf", success, fail);
Morning,
I am using the following to create a file on the local file system. This creates a file if it did not already exist.
function onDeviceReady() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
function gotFS(fileSystem) {
fileSystem.root.getFile("test.txt", {create: true}, gotFileEntry, fail);
}
function gotFileEntry(fileEntry) {
fileEntry.createWriter(gotFileWriter, fail);
}
function gotFileWriter(writer) {
writer.onwrite = function(evt) {
alert("write success");
};
writer.write("We are testing")
}
function fail(error) {
if(error.code == 1){
alert('not found');
}
alert(error.code);
}
However, I need to write to the file ONLY if it did not already exist.
I tried using
function gotFS(fileSystem) {
fileSystem.root.getFile("test.txt", null, gotFileEntry, fail);
}
function gotFS2(fileSystem) {
alert('trying again');
fileSystem.root.getFile("test.txt", {create:true}, gotFileEntry, fail);
}
function fail(error) {
if(error.code == 1){
alert('not found');
gotFS2(fileSystem);
}
alert(error.code);
}
and then calling gotFS2 if error.code == 1
but that did nothing - it didn't even create the file when it didn't exist.
It seems that gotFS2 was not called, yet alert('not found'); in the function fail(error) works.
What's the easiest way to do what I am trying to do?
It seems that problem is in your not saving fileSystem variable (which is in local scope in "gotFS"), so my suggestion is:
var savedFS;
function gotFS(fileSystem) {
savedFS = fileSystem;
fileSystem.root.getFile("test.txt", null, gotFileEntry, fail);
}
function gotFS2(fileSystem) {
alert('trying again');
fileSystem.root.getFile("test.txt", { create: true }, gotFileEntry, function() {});
}
function fail(error) {
if (error.code == 1) {
alert('not found');
gotFS2(savedFS);
}
alert(error.code);
}
How can I create a nested directory in Phonegap with this API?
fileSystem.root.getDirectory("Android/data/com.phonegap.myapp/dir_one/dir_two/", {create:true}, gotDir, onError);
I am using Phonegap 1.8.0 in Android 2.2.
This function will help you create nested dirs.
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
console.log("device is ready");
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
function fail() {
console.log("failed to get filesystem");
}
function gotFS(fileSystem) {
window.FS = fileSystem;
var printDirPath = function(entry){
console.log("Dir path - " + entry.fullPath);
}
createDirectory("dhaval/android/apps", printDirPath);
createDirectory("this/is/nested/dir", printDirPath);
createDirectory("simple_dir", printDirPath);
}
function createDirectory(path, success){
var dirs = path.split("/").reverse();
var root = window.FS.root;
var createDir = function(dir){
console.log("create dir " + dir);
root.getDirectory(dir, {
create : true,
exclusive : false
}, successCB, failCB);
};
var successCB = function(entry){
console.log("dir created " + entry.fullPath);
root = entry;
if(dirs.length > 0){
createDir(dirs.pop());
}else{
console.log("all dir created");
success(entry);
}
};
var failCB = function(){
console.log("failed to create dir " + dir);
};
createDir(dirs.pop());
}
For full example check this gist
Just to add something to dhaval's function: it's not bullet proof for any browser compliant with Javascript Filesystem. If you use it with Chrome and your path is an empty string, it will fail, because apparently, with Chrome, using split() on an empty string returns a one element array, the one element being an epmty string itself, which then causes the directory creation to fail. I modified it in order to correct the problem (there also some other not related changes, for my own purposes):
function createPath(fs, path, callback) {
var dirs = path.split("/").reverse();
var root = fs.root;
var createDir = function(dir) {
if (dir.trim()!="") {
root.getDirectory(dir, {
create: true,
exclusive: false
}, success, function(dir) {
error("failed to create dir " + dir);
});
} else {
callback();
}
};
var success = function(entry) {
root = entry;
if (dirs.length > 0) {
createDir(dirs.pop());
} else {
callback();
}
};
createDir(dirs.pop());
}
This code should do what you want:
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(filesys) {
filesys.root.getDirectory("story_repository", {create: true, exclusive: false}, function(dirEntry) {
dirEntry.getDirectory("dir_name", {create: true, exclusive: false}, function(dirName) {
dirName.getDirectory("img", {create: true, exclusive: false}, function(imgDir) {
console.log("img creation worked");
}, function(error) {
console.log("create img failed");
});
dirName.getDirectory("res", {create: true, exclusive: false}, function(imgDir) {
console.log("res creation worked");
}, function(error) {
console.log("create res failed");
});
}, function(error) {
console.log("create dir_name failed");
})
}, function(error) {
console.log("create story repository failed");
});
}, function(error) {
console.log("request file system failed");
});
I'm using this:
function recursiveGetFile(root, path, opts, success, fail) {
function dir(entry) {
var name = path.shift();
if (path.length > 0)
entry.getDirectory(name, opts, dir, fail);
else
entry.getFile(name, opts, success, fail);
}
path = path.split('/');
dir(root);
}, fail);
There is a simple file manager for cordova-phoengap, with with you can do this and much more:
https://github.com/torrmal/cordova-simplefilemanagement
You can recursively create directories:
//CREATE A DIRECTORY RECURSIVELY as simple as:
new DirManager().create_r('folder_a/folder_b',Log('created successfully'));
Let me know if it helps
I am trying to write a file in android using phonegap cordova 1.5.0. Below is the code snippet. This snippet is working fine in simulator but when I run this on my android mobile it goes till gotFs() then "fail error code" alert of fail() pops up with message
"fail error code 1"
that means it is failing at line
fileSystem.root.getFile("projectFileName", {create: create: true,
exclusive: false}, gotFileEntry, fail);
.
code snippet
function onDeviceReady() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
function gotFS(fS) {
fileSystem = fS;
fileSystem.root.getFile("projectFileName", {create: create: true, exclusive: false}, gotFileEntry, fail);
}
function gotFileEntry(fE) {
fileEntry = fE;
fileEntry.createWriter(gotFileWriter, fail);
}
function gotFileWriter(writer) {
.......... file writing code.
}
function fail(error) {
alert('fail error code = '+error.code);
alert('error '+error);
console.log(error.code);
}
The simulator avd is 2.3.3 and my device has android 2.3.6.
fileSystem.root.getFile("projectFileName", {create: create: true, exclusive: false}, gotFileEntry, fail);
to
fileSystem.root.getFile("projectFileName", {create: true, exclusive: false}, gotFileEntry, fail);