2016-07-27 26 views
0

我想列出用於操作我的HTML的一個特定文件夾中的所有文件。 我正在使用java方法列出要顯示的所有文件。Cordova/Android:在Javascript中使用java方法列出資源中的文件

這是這堂課。

public class JavaScriptInterface { 

private Context context; 

public JavaScriptInterface(Context current){ 
    this.context = current; 
} 

@JavascriptInterface 
public List<String> getFileNames(String path){ 
    String [] files; 

    try { 
     files = context.getAssets().list(path); 
     ArrayList<String> testName = new ArrayList<String>(); 
     for (String file: files) { 
      Log.d("File name: ", file); 
      file = file.replace(".js", ""); 
      String[] fileName = file.split("_"); 
      testName.add(fileName[1]); 
     } 
     return testName; 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return null; 
    } 
} 

} 

然後我在mainActivity

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    super.init(); 

    setToUnstrictMode(); 
    WebView wV = (WebView)appView.getEngine().getView(); 
    wV.addJavascriptInterface(new JavaScriptInterface(this), "jsInterface"); 

    // Set by <content src="index.html" /> in config.xml 
    wV.loadUrl(launchUrl); 
} 

然後我試圖使用此功能訪問它在我的JS加入此。

var getTestNames = function(){ 
    return window.jsInterface.getFileNames("www/js/tests"); 
} 

顯然,它不工作。

編輯:我注意到,我能夠調用該方法,來自方法getFileNames的日誌顯示在我的日誌中。但是,由於某些原因,我無法在變量中正確傳遞它。

回答