0
我正在嘗試爲跨平臺程序Phonegap/Cordova 3.2創建一個Android/Java插件。我正在學習幾個教程,但無法獲得最簡單的插件。Android - Java - 找不到處理意圖的活動
目前我正在研究這個想法,即我的Java代碼在某處是錯誤的。
有人可以請檢查下面的代碼,並告知是否有明顯的錯誤?
我不斷收到的錯誤是
Exception: No Activity found to handle Intent { act=android.intent.action.MEDIA_SCANNER_SCAN_FILE dat=file:///{"fullPath":"media\/test.mp3"} }
這是我的java文件
package org.media.scan;
import java.io.File;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import android.content.Intent;
import android.net.Uri;
public class Scan extends CordovaPlugin {
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
try {
if (action.equals("addRemove")) {
String filePath = args.getString(0);
filePath = filePath.replaceAll("^file://", "");
if (filePath.equals("")) {
callbackContext.error("null path passed");
return false;
}
File file = new File(filePath);
Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
scanIntent.setData(Uri.fromFile(file));
this.cordova.getActivity().startActivity(scanIntent);
callbackContext.success("good");
return true;
} else {
callbackContext.error("invalid action phrase");
}
return false;
} catch(Exception e) {
System.err.println("Exception: " + e.getMessage());
callbackContext.error(e.getMessage());
return false;
}
}
}
我打電話給我的Java代碼與此.js文件的代碼
var Scan = {
createEvent:function (fullPath, successCallback, errorCallback) {
cordova.exec(
successCallback, // success callback function
errorCallback, // error callback function
'Scan', // mapped to our native Java class
'addRemove', // with this action name
[
{
"fullPath":fullPath
}
]
);
}
}
module.exports = Scan;
謝謝陳!修正了代碼,現在它正在返回成功。但是,媒體掃描似乎不起作用,因爲它通過系統沒有顯示爲可供用戶使用。我已確認該文件位於系統文件夾中,但只有在重新啓動手機時纔會顯示。有什麼建議麼? – user1017063
我對這個東西不夠了解。嘗試谷歌。 –
http://androidyue.github.io/blog/2014/01/19/scan-media-files-in-android/看看這篇文章。我認爲它可以解決你的問題。 – androidyue