5

I am making a file explorer in android. So I want when any file is clicked other than directory I want to get a suggestion of apps which can open it and if no app is there then show a dailog. I tried some solutions but nothing worked, so, for now, I am just showing the file is not a directory in the toast

here is part of the code:

protected void onListItemClick(ListView l, View v, int position, long id) {
    String filename = (String) getListAdapter().getItem(position);
    if (path.endsWith(File.separator)) {
        filename = path + filename;
    } else {
        filename = path + File.separator + filename;
    }
    if (new File(filename).isDirectory()) {
        Intent intent = new Intent(this, ListFileActivity.class);
        intent.putExtra("path", filename);
        startActivity(intent);
    } else {
        Toast.makeText(this, filename + " is not a directory", Toast.LENGTH_LONG).show();
    }
}
Swati Garg
  • 995
  • 1
  • 10
  • 21
Rahul Rawat
  • 81
  • 1
  • 9
  • 1
    "I tried some solutions but nothing worked" -- how are we supposed to help you with code that you are not showing us? – CommonsWare May 08 '15 at 19:05

1 Answers1

5

Android has some inbuilt Intent Action Type which helps you to Open or View specific files, but for this you need to know which type of file you are going to handle.

Suppose If you have file type which categorized in document type you can use,

ACTION_OPEN_DOCUMENT with specific MIME_TYPE (Android 4.4 or Higher)

or If you going to handle some media file (Audio/Video)

you can use,

ACTION_VIEW

To identify MIME_TYPE of specific file you can use function

guessContentTypeFromName (String url)Link

Or getMimeTypeFromExtension(String extension)Link

user370305
  • 108,599
  • 23
  • 164
  • 151
  • 2
    Older post but quick reference http://stackoverflow.com/questions/6265298/action-view-intent-for-a-file-with-unknown-mimetype. I suggest you to go with latest `Intent Action`. – user370305 May 08 '15 at 19:21