Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.

Get Available Free Disk Space Using Apache Cordova

TwitterFacebookRedditLinkedInHacker News

It was brought to my attention that the File plugin for Apache Cordova has no documented methods for finding the available disk space on a device.

This does not mean that you cannot figure out how much space is available on your users device because you can make use of Apache Cordova’s nifty cordova.exec function.

In your Apache Cordova, Phonegap, or Ionic Framework project, make sure that you have the Apache Cordova File plugin installed. If you’re not sure how to do this, using a command prompt or Terminal, navigate to your project’s root and run the following:

cordova plugin add org.apache.cordova.file

You should already have the Android or iOS platform added before trying to install the plugin as it could result in strange behavior if you try to add a platform after.

Now let’s take a look at the following:

cordova.exec(function(result) {
    alert("Free Disk Space: " + result);
}, function(error) {
    alert("Error: " + error);
}, "File", "getFreeDiskSpace", []);

The above code will call the getFreeDiskSpace method of the File plugin and depending on the success or error callback, give you a result. If the success callback is reached, you’ll get the total free disk space in kilobytes. You can easily convert to megabytes or gigabytes with simple math.

A video version of this article can be seen below.

Nic Raboy

Nic Raboy

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in C#, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Unity. Nic writes about his development experiences related to making web and mobile development easier to understand.