Android – Using Intents to Open Files

Do you want to open from your code some mp3 or image file with the default media player/image viewing application? Use this:

File videoFile2Play = new File("/sdcard/nice_movie.mpeg");
Intent i = new Intent();
i.setAction(android.content.Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(videoFile2Play), "video/mpeg");
startActivity(i);
File musicFile2Play = new File("/sdcard/some_file.mp3");
Intent i2 = new Intent();
i2.setAction(android.content.Intent.ACTION_VIEW);
i2.setDataAndType(Uri.fromFile(musicFile2Play), "audio/mp3");
startActivity(i2);

If you want to open some other supported files with their own applications take a look at the table below to see the exact types you should use. If you don’t find in the table the file extension you want don’t worry, use the type String with the wild card character like this (and if your system supports that file it will be loaded by the appropriate application):

File videoFile2Play2 = new File("/sdcard/nice_movie2.mp4");
i.setDataAndType(Uri.fromFile(videoFile2Play2), "video/*");
startActivity(i);
Extension MIME Type
Android Application .apk application/vnd.android.package-archive
Text .txt text/plain
.csv text/csv
.xml text/xml
Web related .htm text/html
.html text/html
.php text/php
Image .png image/png
.gif image/gif
.jpg image/jpg
.jpeg image/jpeg
.bmp image/bmp
Audio .mp3 audio/mp3
.wav audio/wav
.ogg audio/x-ogg
.mid audio/mid
.midi audio/midi
.amr audio/AMR
Video .mpeg video/mpeg
.3gp video/3gpp
Package .jar application/java-archive
.zip application/zip
.rar application/x-rar-compressed
.gz application/gzip

For more info on MIME types and files handling based on their extension take a look at the MIMETypeMap documentation.

7 comments

  1. Nitan Sehgal

    import android.webkit.MimeTypeMap;

    try
    {
    Intent myIntent = new Intent(android.content.Intent.ACTION_VIEW);
    File file = new File(aFile.getAbsolutePath());
    String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString());
    String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    myIntent.setDataAndType(Uri.fromFile(file),mimetype);
    startActivity(myIntent);
    }
    catch (Exception e)
    {
    // TODO: handle exception
    String data = e.getMessage();
    }

  2. To open file from your android application:

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

    i have successful experience with Second Method written over there…..

  3. Just desire to say your article is as astonishing. The clearness in your post is simply excellent and i can assume you’re an expert on this subject. Well with your permission allow me to grab your feed to keep up to date with forthcoming post. Thanks a million and please carry on the rewarding work.

  4. Hami Nick

    Thank you for detail mimetype.
    @Nitan Sehgal: Your solution is for common cases and it is helpful. Thanks.

Leave a Reply