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);
Related
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);
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);
});
How do I write html to a text file using Phonegap? I'm trying to follow the example in the API documentation. The file creating works but I can't get console.log to work.
function gotFS(fileSystem) {
fileSystem.root.getFile("readme.txt", {create: true, exclusive: false}, gotFileEntry, fail);
}
function gotFileEntry(fileEntry) {
fileEntry.createWriter(gotFileWriter, fail);
}
function gotFileWriter(writer) {
writer.onwriteend = function(evt) {
console.log($("h1"));
};
}
function fail(error) {
console.log(error.code);
}
<body>
<h1>Example</h1>
<p>Write File</p>
</body>
Use "document.write" instead. It worked for me.
try to use
Log.d("info", "YOUR_TEXT");
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 save an image to gallery of the mobile with android phonegap?
I tried this code:
function save(){
document.addEventListener("deviceready", onDeviceReady, false);
}
// PhoneGap is ready
//
function onDeviceReady() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
function gotFS(fileSystem) {
fileSystem.root.getFile("pic.jpg", {create: true, exclusive: false}, gotFileEntry, fail);
}
function gotFileEntry(fileEntry) {
console.log(fileEntry.fullPath);
fileEntry.createWriter(gotFileWriter, fail);
}
function gotFileWriter(writer) {
var photo = document.getElementById("dwnldImg");
writer.write(photo.value);
}
But it saves the image into my project directory which is file:///data/data/pack.name/pic.jpg.
how can i make it appear in the mobile gallery
try using this:
function capturePhoto() {
// Take picture using device camera and retrieve image as base64-encoded string
navigator.camera.getPicture(onPhotoDataSuccess, onFail,{
quality : 25,
destinationType : Camera.DestinationType.FILE_URI,
sourceType : Camera.PictureSourceType.CAMERA,
allowEdit : true,
encodingType: Camera.EncodingType.JPEG,
popoverOptions: CameraPopoverOptions,
saveToPhotoAlbum: true });
}