To create a plugin from it, here it is
scanMedia.java
public class scanMedia extends CordovaPlugin {
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if (!action.equals("mediaScanner")) {
return false;
}
try {
String absolutePath = args.getString(0);
if (absolutePath.startsWith("data:image")) {
absolutePath = absolutePath.substring(absolutePath.indexOf(',') + 1);
}
return this.mediaScanner(absolutePath, callbackContext);
} catch (JSONException e) {
e.printStackTrace();
callbackContext.error(e.getMessage());
return false;
} catch (InterruptedException e) {
e.printStackTrace();
callbackContext.error(e.getMessage());
return false;
}
}
private boolean mediaScanner(String absolutePath, CallbackContext callbackContext) throws InterruptedException, JSONException
{
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.parse(absolutePath.toString());
mediaScanIntent.setData(contentUri);
System.out.println("from internal?" + contentUri);
this.cordova.getActivity().sendBroadcast(mediaScanIntent);
return true;
} }
scanMedia.js
(function() {
var cordovaRef = window.PhoneGap || window.Cordova || window.cordova;
function scanMedia(){ }
scanMedia.prototype.mediaScanner = function(string, win, fail) {
cordovaRef.exec(win, fail, "scanMedia", "mediaScanner", [string]);
};
cordovaRef.addConstructor(function() {
if (!window.plugins) {
window.plugins = {};
}
if (!window.plugins.scanMedia) {
window.plugins.scanMedia = new scanMedia();
}
});
})();
and link in config.xml file
<plugin name="scanMedia" value="org.apache.cordova.scanMedia"/>
To call the plugin, you need to pass the .fullPath file to the function ( you are using a file reader with phone records )
var theLink = fileEntry.fullPath;
window.plugins.scanMedia.mediaScanner(theLink,
function(result) {
console.log(result);
}, function(error) {
console.log(error);
});
, -, . - iOS, :)