Open file with default application using Intents

4 votes · 3 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

3 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.