Phonegap - 如何在创建文件后触发媒体扫描仪 [英] Phonegap - How to trigger media scanner after creating file
问题描述
我有以下代码(这是一个插件,将base64保存为手机上的图像文件)
I have the following code (this is a plugin to save a base64 as an image file on the phone)
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
if (!action.equals("saveImage")) {
return false;
}
try {
String b64String = args.getString(0);
if (b64String.startsWith("data:image")) {
b64String = b64String.substring(b64String.indexOf(',') + 1);
}
JSONObject params = args.getJSONObject(1);
//Optional parameter
String filename = params.has("filename")
? params.getString("filename")
: "drawsaur_" + System.currentTimeMillis() + ".png";
String folder = params.has("folder")
? params.getString("folder")
: Environment.getExternalStorageDirectory() + "/drawsaur";
Boolean overwrite = params.has("overwrite")
? params.getBoolean("overwrite")
: false;
return this.saveImage(b64String, filename, folder, overwrite, 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 saveImage(String b64String, String fileName, String dirName, Boolean overwrite, CallbackContext callbackContext) throws InterruptedException, JSONException {
try {
//Directory and File
File dir = new File(dirName);
if (!dir.exists()) {
dir.mkdirs();
}
File file = new File(dirName, fileName);
//Avoid overwriting a file
if (!overwrite && file.exists()) {
callbackContext.error("File already exists!");
return true;
}
//Decode Base64 back to Binary format
byte[] decodedBytes = Base64.decode(b64String.getBytes());
//Save Binary file to phone
file.createNewFile();
FileOutputStream fOut = new FileOutputStream(file);
fOut.write(decodedBytes);
fOut.close();
callbackContext.success("Saved successfully to " + dirName + "/" + fileName);
return true;
} catch (FileNotFoundException e) {
callbackContext.error("File not Found!");
return false;
} catch (IOException e) {
callbackContext.error(e.getMessage());
return false;
}
}
我需要在文件上触发媒体扫描将在图库中显示该文件。我如何做到这一点?我使用Phonegap 2.9.0并在nexus4上测试
I need to trigger media scan on the file so android will show the file in gallery. How can I do this ? I am using Phonegap 2.9.0 and testing on nexus4
谢谢
推荐答案
可以从中创建一个插件,
Got to create a plugin from it , here it is
scanMedia.java
the 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
the 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文件中引用
and reference in the config.xml file
<plugin name="scanMedia" value="org.apache.cordova.scanMedia"/>
为了调用插件,你需要将file.fullPath传递给函数=http://docs.phonegap.com/en/2.5.0/cordova_file_file.md.html#FileReader =nofollow>您使用phonegap文件阅读器)
In order to call the plugin you need to pass the file.fullPath to the function (you use the phonegap file reader)
var theLink = fileEntry.fullPath;
window.plugins.scanMedia.mediaScanner(theLink,
function(result) {
console.log(result);
}, function(error) {
console.log(error);
});
希望它帮助需要媒体发现的人。如果任何人都可以这样做的iOS是伟大的:)
Hope it helps somebody in need of media discovery. If anybody can do this for iOS that would be great :)
这篇关于Phonegap - 如何在创建文件后触发媒体扫描仪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!