Question

I am working on an Android application and I have a small problem. I have a list of html files in the assests folder, all named accordingly to country names such as "United Kingdom.html" and I have user selecting country name from a listview, I fetch the country name from the list and save it as a string called "country_name" then pass it on to different activity as a "value" string. I am trying to come up with a solution to basically checking if the file with the same name exists in the assets folder and if so use webview to open up that file. The code below is what I've got so far.

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        String value = extras.getString("country_name");
        result_info = (TextView) findViewById(R.id.result_info);
        result_info.setText(value);
        try {
            Arrays.asList(getResources().getAssets().list("")).contains(value);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        webview = (WebView) findViewById(R.id.webview);
        webview.loadUrl("file:///android_asset/United Kingdom.html");
}

Thanks in advance.


EDIT: I might have found a possible solution. Basically get the list of the file names as an array of strings then compare it to my string ("value" + ".html").

final AssetManager assetManager = getAssets();
    try {
        // for assets folder add empty string
                    String[] filelist = assetManager.list("");
                    // for assets/subFolderInAssets add only subfolder name
                    String[] filelistInSubfolder = assetManager.list("subFolderInAssets");
        if (filelist == null) {
            // dir does not exist or is not a directory
        } else {
            for (int i=0; i<filelist.length; i++) {
                // Get filename of file or directory
                String filename = filelist[i];
            }
        }

                    // if(filelistInSubfolder == null) ............  

    } catch (IOException e) {
        e.printStackTrace();
    }

EDIT 2: SOLUTION

Thanks to Venkatesh I got it working. His solution required few changed, but I got it working in the end by having the following code:

        AssetManager am = getAssets();
        String extension = ".html";
        try {
            String fileList[] = am.list("");
            //checking if any file gets returned from the array
            //result_info.setText(fileList[1]);
            for (String fileName : fileList) {

                  if (fileName.equals(value+extension)) {  
                      //checking what the filename looks like 
                      //result_info.setText(fileName);
                      webview = (WebView) findViewById(R.id.webview);
                      webview.loadUrl("file:///android_asset/"+fileName);
                   }
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
Was it helpful?

Solution

Try this,

AssetManager am = getAssets();
String fileList[] = am.list("");

 for (String fileName : fileList) {

  if (fileName.equals(value+"addExtensions")) {  
     webview.loadUrl("file:///android_asset/"+filename);
   }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top