Open file with default application using Intents

7 votes · 10 comments

This code allows you to open a particular file with the default application, specifying the MIME, in these cases audio and video.

raw ·
copy
· download
Intent intent = new Intent(); intent.setAction(android.content.Intent.ACTION_VIEW); File file = new File("/sdcard/test.mp4"); intent.setDataAndType(Uri.fromFile(file), "video/*"); startActivity(intent); Intent intent = new Intent(); intent.setAction(android.content.Intent.ACTION_VIEW); File file = new File("/sdcard/test.mp3"); intent.setDataAndType(Uri.fromFile(file), "audio/*"); startActivity(intent);
Add a comment

10 Comments

Other formats how to do? For example: .txt, .doc, .zip

Reply · Sept. 25, 2009, 1:44 p.m.

@zx012345, for text formats you use "text/*", others as .doc and .zip depends on which is the default application

Reply · July 29, 2011, 2 p.m.

http://helloworldcodes.blogspot.com/2011/10/android-open-folder-with-default.html

here you can find the way to open a file even if you don't know the mime type of any file. i have successful experience with second method listed there.

Reply · Dec. 10, 2011, 1:09 p.m.

First time be here, nice website.

Reply · March 3, 2012, 2:59 a.m.

That website not related to Android intents, unless you're trying to buy purses using intents :D

Reply · Feb. 12, 2014, 5:24 a.m.

Thanks a lot, that was very helpful

Reply · July 24, 2014, 2:11 p.m.

I am so glad that I have bookmarked this website because I see that it is full of various and attractive information about Html Coding. Thanks one more time for this publication, it was really interesting to update. Web Design Dubai

Reply · April 12, 2012, 4:14 p.m.

This code is not working for .torrent files. Kindly tell me what will be the code. I want to select between installed torrent supported fapps (utorrent.bittorrent)

Reply · Oct. 25, 2013, 5:54 a.m.

This code is not working for .torrent files. Kindly tell me what will be the code. I want to select between installed torrent supported fapps (utorrent.bittorrent)

Reply · Oct. 25, 2013, 5:55 a.m.