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.