2012年7月13日 星期五

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:
?
01
02
03
04
05
06
07
08
09
10
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):
?
1
2
3
File videoFile2Play2 = new File("/sdcard/nice_movie2.mp4");
i.setDataAndType(Uri.fromFile(videoFile2Play2), "video/*");
startActivity(i);

ExtensionMIME Type
Android Application.apkapplication/vnd.android.package-archive
Text.txttext/plain
.csvtext/csv
.xmltext/xml
Web related.htmtext/html
.htmltext/html
.phptext/php
Image.pngimage/png
.gifimage/gif
.jpgimage/jpg
.jpegimage/jpeg
.bmpimage/bmp
Audio.mp3audio/mp3
.wavaudio/wav
.oggaudio/x-ogg
.midaudio/mid
.midiaudio/midi
.amraudio/AMR
Video.mpegvideo/mpeg
.3gpvideo/3gpp
Package.jarapplication/java-archive
.zipapplication/zip
.rarapplication/x-rar-compressed
.gzapplication/gzip

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

沒有留言:

張貼留言