Pedro Carvalho
Pedro Carvalho

Reputation: 3

Cordova or Ionic for write/read files on android?

I am new on mobile development and I need some help. I want to delevop a app that writes and read a .txt file on android. I am just not sure if I use cordova or ionic to do that. What do you guys recommend? And if there is any example of how to do that, I would appreciate a lot!

Thank you

Upvotes: 0

Views: 2853

Answers (3)

Chanaka Weerasinghe
Chanaka Weerasinghe

Reputation: 5742

yes it can be done easily

here is working Environment

https://ionicframework.com/docs/native/file

Upvotes: 0

Nidhin Joseph
Nidhin Joseph

Reputation: 10237

Ionic ships with many built-in components, that in most cases gives a UX/UI closer to native components. This is the basic advantage of using ionic.

The fact is, Ionic is built on top of Cordova, so you cannot argue that ionic runs better than Cordova itself. Ionic is like steroids that you can give to your Cordova apps. If you do not need ionic components, then you need to start as a Cordova project.

enter image description here

File Plugin

The cordova-plugin-file implements a File API allowing read/write access to files residing on the device.

cordova plugin add cordova-plugin-file

Write to a file Check here

function writeFile(fileEntry, dataObj) {
    // Create a FileWriter object for our FileEntry (log.txt).
    fileEntry.createWriter(function (fileWriter) {

        fileWriter.onwriteend = function() {
            console.log("Successful file write...");
            readFile(fileEntry);
        };

        fileWriter.onerror = function (e) {
            console.log("Failed file write: " + e.toString());
        };

        // If data object is not passed in,
        // create a new Blob instead.
        if (!dataObj) {
            dataObj = new Blob(['some file data'], { type: 'text/plain' });
        }

        fileWriter.write(dataObj);
    });
}

Read a file Check here

function readFile(fileEntry) {

    fileEntry.file(function (file) {
        var reader = new FileReader();

        reader.onloadend = function() {
            console.log("Successful file read: " + this.result);
            displayFileData(fileEntry.fullPath + ": " + this.result);
        };

        reader.readAsText(file);

    }, onErrorReadFile);
}

Upvotes: 1

Milad Dehghan
Milad Dehghan

Reputation: 306

  • Apache Cordova is a community project, letting you build mobile apps for various mobile platforms with one unique code base, as you develop your app with web technologies (HTML5, Javascript and CSS3) instead of relying on platform-specific (native) APIs like those of Android, iOS, or Windows Phone.

  • Ionic Framework is a set of css classes and a library of Javascript directives and modules, built on top of Cordova, with AngularJS.

  • If you want to speed up your work and want to use some premade UI elements ionic is a way to go

  • and If you want to build from scratch Cordova is better option.

When using these technologies. you need good Javascript knowledge. in your case of reading files both cordova and ionic has plugin for that.

see cordova-plugin-file for more information

Hope it helps

definitions from this link

Upvotes: 0

Related Questions