如何扫描从网上下载的文件?我已成功从url下载文件并将其保存到文件夹但是当我打开媒体播放器时没有媒体。经过一点点搜索,我刚刚看到帖子,下载文件后,它必须扫描。所以我尝试了类似的东西
public class TagsPlugin extends CordovaPlugin {
private CallbackContext callbackContext;
private static Context context;
private MediaScannerConnection conn;
@Override
public boolean execute(final String action, final JSONArray args, final CallbackContext callbackContext) throws JSONException {
this.callbackContext = callbackContext;
final String songName = args.getString(0) + " " + myDomain;
cordova.getActivity().runOnUiThread(new Runnable() {
public void run() {
String resp;
final MediaScannerConnection conn;
final String newFilePath = action;
String filePath = action;
AudioFile audioFile = null;
Tag audioTag = null;
AudioHeader audioHeader = null;
try{
TagOptionSingleton.getInstance().setAndroid(true);
AudioFile f = (MP3File) AudioFileIO.read(new File(filePath));
audioTag = f.getTag();
audioTag.setField(FieldKey.TITLE, songName);
f.commit();
resp = audioTag.getFirst(FieldKey.TITLE);
File file = new File(filePath);
MediaScannerConnection.scanFile(context, new String[] { file.getAbsolutePath() }, null, null);
//MediaScannerConnection.scanFile(context, new String[] { filePath }, null, null);
}catch (Exception e){
StringWriter errors = new StringWriter();
e.printStackTrace(new PrintWriter(errors));
resp = errors.toString();
}
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, resp);
pluginResult.setKeepCallback(true);
callbackContext.sendPluginResult(pluginResult);
}
});
return true;
}
}
但它显示以下错误
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.content.Context.bindService(android.content.Intent, android.content.ServiceConnection, int)' on a null object reference
at android.media.MediaScannerConnection.connect(MediaScannerConnection.java:119)
at android.media.MediaScannerConnection.scanFile(MediaScannerConnection.java:238)
at TagsPlugin$1.run(TagsPlugin.java:91)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
我做错了什么?我只想在android中的媒体播放器中显示下载的歌曲。
是否有任何与我正在做的相同的phonegap / cordova插件?
答案 0 :(得分:0)
我自己使用下面的代码完成了。
MediaScannerConnection.scanFile(this.cordova.getActivity(),
new String[] { filePath }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
cbContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, uri.toString()));
}
});