Where is the location of file created by Cordova File Plugin?
Asked Answered
C

2

30

I have used Cordova File Plugin to create files on mobile device. Below is the code to create files:

window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function (dir) {
   alert(cordova.file.dataDirectory);
   dir.getFile("log.txt", { create: true }, function (file) {
          alert("got the file: "+ file.name + ', ' + file.fullPath);
   });
});

When I deploy the app on my android phone, the file will create successfully, but I can't find the created file on my device.

Although cordova.file.dataDirectory is pointing to file:///data/data/io.cordova.myappId/files/ path on my device, the io.cordova.myappId folder doesn't exist in data>data path, but exists in Android>data path. By the way, I checked both storage>Android>data>io.Cordova.myappId>files & storage>data>data and the file doesn't exist.

Is this because:

The created file is located in another place, so where can I find that?

or

Because it's private and my file manager doesn't have access to it, so how can I change the permission setting to have a public file?

Colangelo answered 9/1, 2016 at 13:38 Comment(0)
C
44

Why I can't find the file on my device?

The file is created successfully but I can't find that on my device because the dataDirectory path which I indicates to create the file, is a private path and my device file manager doesn't have access to it (base on this table). Actually dataDirectory is an Internal Storage option.

Internal Storage: Store private data on the device memory.

You can save files directly on the device's internal storage. By default, files saved to the internal storage are private to your application and other applications cannot access them (nor can the user). When the user uninstalls your application, these files are removed.(reference)

How to create a public file?

So, to create a file on my device that I can access it with the file manager, I have to use one of public path options like:externalDataDirectory. Before, I was thinking it is for storing files on an external SD Card that I had not on my phone, so I didn't test it. But testing it today, it creates the file on my internal device storage in Android/data/<app-id>/files path which is public and I can access it with device file manager.

Actually the term internal and external was misleading for me while external storage can be a removable storage media (such as an SD card) or an internal (non-removable) storage(reference).

Colangelo answered 11/1, 2016 at 11:50 Comment(6)
Hi @A.M, i facing the similar issue. Though i use externalDataDirectory, i am unable to find my file in the device. Function getFile returns success. Am i missing anything else. Please help.Endotoxin
@Aish123 I don't have your code so I can't tell you if something is missed or not. Just can tell you this will work: window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function (dirEntry) { dirEntry.getDirectory(dirName, { create: true }, function (dirEntry) { dirEntry.getFile(fileName, { create: true }, function (fileEntry) {alert("Create the file: " + fileEntry.name + ', ' + fileEntry.fullPath);}); }, errorHandler) }, errorHandler);Colangelo
@Aish123 Also check the other r/w paths base on this table if your device is android or check other tables based on your device OS.Colangelo
Thanks for reply @A.M. I have used externalRootDirectory but the file got created in internal storage though the callback shows external memory as path. All i want is to create a file for checking logs and i got it. Thanks again for the help :)Endotoxin
@A.M: Thanks to you. I was facing this issue yesterday when I updated my phonegap version from 5.x.x to 6.3.0. Somewhere between these versions the behaviour has to be changed, because with version 5.x.x I received the path from device storage with dataDirectory.Fayola
I only see the file generated on the phone, but not on the emulator.Jabe
E
2

Reading/wiring files in Cordova is painful. I wrote a script that uses promises to make this easier.

Add cordova-file-storage.js to your project, then do the following:

// the object you want to save
var objectToSave = { firstName: 'John', lastName: 'Doherty' };

// the filename and extension you want to use
var filename = 'whatever.txt';

// the data to write (convert JSON object to a string)
var data = JSON.stringify(objectToSave);

// call write with params, .then executes with the filePath once complete
fileStorage.write(filename, data).then(function(filePath) {
     console.log(filePath);
})
.catch(function(err) {
     // this executes if something went wrong
     console.warn(err);
});

It uses external storage by default. To use sandboxed storage inaccessible to the user add true as the last param.

Elonore answered 15/11, 2019 at 13:27 Comment(4)
The link is brokenTarp
Thanks @Will, it's now fixedElonore
Still getting a github 404 error on that link.Tarp
Apologies, it was private :-| sorted nowElonore

© 2022 - 2024 — McMap. All rights reserved.