Cordova Phonegap-创建文件后如何触发媒体扫描程序
我有以下代码(这是一个将base64保存为手机图像文件的插件) 我需要在文件上触发媒体扫描,以便android在gallery中显示文件。我该怎么做?我正在使用PhoneGap2.9.0并在nexus4上进行测试Cordova Phonegap-创建文件后如何触发媒体扫描程序,cordova,media,android-mediascanner,Cordova,Media,Android Mediascanner,我有以下代码(这是一个将base64保存为手机图像文件的插件) 我需要在文件上触发媒体扫描,以便android在gallery中显示文件。我该怎么做?我正在使用PhoneGap2.9.0并在nexus4上进行测试 谢谢你我要用它创建一个插件,在这里 scanMedia.java public class scanMedia extends CordovaPlugin { @Override public boolean execute(String action, JSONArray args
谢谢你我要用它创建一个插件,在这里 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);
//File f = new File(filename);
Uri contentUri = Uri.parse(absolutePath.toString());
mediaScanIntent.setData(contentUri);
System.out.println("from internal?" + contentUri);
//this.cordova.getContext().sendBroadcast(mediaScanIntent); //this is deprecated
this.cordova.getActivity().sendBroadcast(mediaScanIntent);
return true;
} }
scanMedia.js
(function() {
/* This increases plugin compatibility */
var cordovaRef = window.PhoneGap || window.Cordova || window.cordova; // old to new fallbacks
/**
* The Java to JavaScript Gateway 'magic' class
*/
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();
}
});
})();
和config.xml文件中的引用
<plugin name="scanMedia" value="org.apache.cordova.scanMedia"/>
希望它能帮助那些需要媒体发现的人。如果有人能在iOS上做到这一点,那就太好了:)要用它创建一个插件,就在这里 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);
//File f = new File(filename);
Uri contentUri = Uri.parse(absolutePath.toString());
mediaScanIntent.setData(contentUri);
System.out.println("from internal?" + contentUri);
//this.cordova.getContext().sendBroadcast(mediaScanIntent); //this is deprecated
this.cordova.getActivity().sendBroadcast(mediaScanIntent);
return true;
} }
scanMedia.js
(function() {
/* This increases plugin compatibility */
var cordovaRef = window.PhoneGap || window.Cordova || window.cordova; // old to new fallbacks
/**
* The Java to JavaScript Gateway 'magic' class
*/
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();
}
});
})();
和config.xml文件中的引用
<plugin name="scanMedia" value="org.apache.cordova.scanMedia"/>
希望它能帮助那些需要媒体发现的人。如果有人能为iOS做到这一点,那就太好了:)不幸的是,我几乎没能用网络上的一些代码制作这一个,所以我不太擅长iTunes。不幸的是,我几乎没能用网络上的一些代码制作这一个,所以我不太擅长它