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

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.setDataAndType(Uri.fromFile(videoFile2Play), "video/mpeg");
File musicFile2Play = new File("/sdcard/some_file.mp3");
Intent i2 = new Intent();
i2.setDataAndType(Uri.fromFile(musicFile2Play), "audio/mp3");

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/*");
Extension MIME Type
Android Application .apk application/
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.

Related Posts


  1. Nitan Sehgal
    June 20, 2011 10:01 Reply

    import android.webkit.MimeTypeMap;

    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);
    catch (Exception e)
    // TODO: handle exception
    String data = e.getMessage();

    • December 12, 2012 23:52 Reply

      Great example, it worked perfectly on my Droid 2.3.3

    • @Nitan Sehgal
      February 14, 2013 10:06 Reply

      ty mate, works like charm

  2. mia
    October 18, 2011 16:24 Reply

    To open file from your android application:

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

  3. July 17, 2013 11:57 Reply

    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
    October 09, 2013 04:49 Reply

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

Leave a reply