Rahul Rawat
Rahul Rawat

Reputation: 81

Open file with default applications in android

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();
    }
}

Upvotes: 5

Views: 9527

Answers (1)

user370305
user370305

Reputation: 109257

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

Upvotes: 5

Related Questions