This simple Cordova plugin allows a lot of functionality, namely, storing and reading files from external and internal storage, saving settings, creating toast messages, and getting the sdk version of the android phone.
Writing files to external storage works good for api level 29 and above.
- To write a text file to internal storage:
cordova.plugins.CordovaAndroidFuns.store('hello.txt', 'Hello world',
function (success){
// It worked
},
function (error){
// Did not work
}
);
- To read a file from internal storage:
cordova.plugins.CordovaAndroidFuns.read('hello.txt',
function (response){
// Success
console.log(response);
},
function (error){
console.log(error);
}
);
- To delete a file from internal storage:
cordova.plugins.CordovaAndroidFuns.deleteFile('hello.txt',
function(success){
// Success
},
function(error){
// Error
}
);
- To append text to file:
cordova.plugins.CordovaAndroidFuns.appendFile('hello.txt',
function(success){
// Success
},
function(error){
//Error
}
);
- To save a string preference:
cordova.plugins.CordovaAndroidFuns.storePS('key', 'value',
function(success){
// Success
},
function(error){
// Error
}
);
- To save a boolean preference:
cordova.plugins.CordovaAndroidFuns.storePB('key', true,
function(success){
// Success
},
function(error){
// Error
}
);
- To save an int preference:
cordova.plugins.CordovaAndroidFuns.storePI('key', 0,
function(success){
// Success
},
function(error){
// Error
}
);
- To save an float preference:
cordova.plugins.CordovaAndroidFuns.storePF('key', 0.0,
function(success){
// Success
},
function(error){
// Error
}
);
- To save an long preference:
cordova.plugins.CordovaAndroidFuns.storePL('key', 0,
function(success){
// Success
},
function(error){
// Error
}
);
- To get a preference:
cordova.plugins.CordovaAndroidFuns.getPref('key', 'type', // type can be int, float, boolean, long, or String
function(response){
// Success
console.log(response);
},
function(error){
// Error
console.log(error);
}
);
- To show a short toast:
cordova.plugins.CordovaAndroidFuns.showShortToast(message);
- To show a long toast:
cordova.plugins.CordovaAndroidFuns.showLongToast(message);
- To store an image to external storage:
cordova.plugins.CordovaAndroidFuns.storeImage(base64string, 'Pictures', `${fileName}.png`,
function(success){
// Success
},
function(error){
// Error
}
);
- To store a document to external storage:
cordova.plugins.CordovaAndroidFuns.storeDocument(fileContent, mimeType, 'Download', fileName,
function(success){
// Success
},
function(error){
// Error
}
);
- To store an audio to external storage:
cordova.plugins.CordovaAndroidFuns.storeAudio(base64string, 'audio/mp3', 'Music', 'hello.mp3',
function(success){
alert(success);
},
function(error){
alert(error);
}
);
- To store an video to external storage:
cordova.plugins.CordovaAndroidFuns.storeVideo(base64string, 'video/mp4', 'Movies', 'hello.mp4',
function(success){
alert(success);
},
function(error){
alert(error);
}
);
- Get data from external file:
cordova.plugins.CordovaAndroidFuns.getDataFromFile(mimeType,
function(response){
console.log(response);
},
function(error){
console.log(error);
}
);
- Get sdk version of android phone:
cordova.plugins.CordovaAndroidFuns.getDeviceVersion(
function(deviceVersion){
console.log(deviceVersion);
},
function(error){
console.log(error);
}
);