2

I'm working on a program which needs to show list file in ListView. I got this list with path_of_files. But this is what i want to ask: when i clicked on an item, how can user open it? I think i want a popup let user choose which program user want to open it. For example: I have a path: /sdcard/file.xls. When user clicked in, a popup shows some application like: Microsoft Excel, Google Spreadsheet (I only want to work with xls - and it only works with it, still ok). How can i do it?

File sdCardRoot = Environment.getExternalStorageDirectory();
        File yourDir = new File(sdCardRoot, "directory");
        for (File f : yourDir.listFiles()) {
            if (f.isFile()) {
                String name = f.getName();
                Long lastModified = f.lastModified();
                if (name.toLowerCase().contains(".xls"))
                    fileObjList.add(new FileObj(0,name,lastModified));
            }
        }

This is my code to get filename and file path.

Harry T.
  • 3,478
  • 2
  • 21
  • 41
  • 1
    Refer to this http://stackoverflow.com/questions/28978581/how-to-make-intent-settype-for-pdf-xlsx-and-txt-file-android – void May 19 '16 at 05:06

1 Answers1

1

First , set a listener to your listView Items . And inside the handler callback you can do something likebelow to open up excels :

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/vnd.ms-excel");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);


try {
    startActivity(intent);
} 
catch (ActivityNotFoundException e) {
    Toast.makeText(context, "No Application Available to View Excel", Toast.LENGTH_SHORT).show();
}
Vivek_Neel
  • 1,343
  • 1
  • 14
  • 25
  • It works like a charm. Thanks. But can you tell me how to do the same if i want to open a folder path by choosing an explorer app? – Harry T. May 19 '16 at 05:46