废话不多说直接上代码
1.编写各种文件类型和后缀名数组:
//文件类型
public static final int TYPE_FOLDER = 0;
public static final int TYPE_FILE = 1;
//public static final int TYPE_PICTURE = 0;
public static final int TYPE_VIDEO = 2;
public static final int OPEN_FILE = 10;
public static final int TYPE_AUDIO = 13;
public static final int TYPE_PICTURE = 14;
public static final int TYPE_DOC = 21;
public static final int TYPE_XLS = 22;
public static final int TYPE_PPT = 23;
public static final int TYPE_PDF = 24;
public static final int TYPE_TXT = 25;
/** 视频格式 */
public static final String[] MIME_TYPE_VIDEO_ALL = { ".3gp", ".3gpp", ".divx",
".h264", ".avi", ".m2ts", ".mkv", ".mov", ".mp2", ".mp4", ".mpg",
".mpeg", ".wmv", ".ts", ".tp" , ".vob",
".flv", ".vc1", ".m4v", ".f4v", ".asf", ".lst", ".lsf", ".lsx",
".mng", ".asx", ".wm", ".wmx", ".wvx", ".movie", ".3g2", ".dl",
".dif", ".dv", ".fli", ".qt", ".mxu",".webm", "mkv", "rmvb"};
/** 音频格式 */
public static final String[] MIME_TYPE_MUSIC = { ".mp3", ".wma", ".m4a",
".aac", ".ape", ".ogg", ".flac", ".alac", ".wav", ".mid", ".xmf",
".mka", ".pcm", ".adpcm", ".snd", ".midi", ".kar", ".mpga",
".mpega", ".mp2", ".m3u", ".sid", ".aif", ".aiff", ".aifc", ".gsm",
".m3u", ".wax", ".ra", ".ram", ".pls", ".sd2","amr","wv","mmf","m4r"};
/** 图片格式 */
public static final String[] MIME_TYPE_PHOTO = { ".jpg", ".jpeg", ".bmp",
".tif", ".tiff", ".png", ".gif", ".giff", ".jfi", ".jpe", ".jif",
".jfif", ".cur", ".ico", ".ief", ".jpe", ".pcx", ".svg", ".svgz",
".djvu", ".djv", ".wbmp", ".ras", ".cdr", ".pat", ".cdt", ".cpt",
".art", ".jng", ".psd", ".pnm", ".pbm", ".pgm", ".ppm", ".rgb",
".xbm", ".xpm", ".xwd" };
2.传人文件名返回文件类型
/** 文件类型 */
public static int getFileType(String filename){
if(filename.contains(".")){
String type = filename.substring(filename.lastIndexOf("."));
//Utils.MyLog("file: " + name + " type: " + type);
if(type.equalsIgnoreCase(".doc") || type.equalsIgnoreCase(".docx")){
return Utils.TYPE_DOC;
}else if(type.equalsIgnoreCase(".xls") || type.equalsIgnoreCase(".xlsx")){
return Utils.TYPE_XLS;
}else if(type.equalsIgnoreCase(".ppt") || type.equalsIgnoreCase(".pptx")){
return Utils.TYPE_PPT;
}else if(type.equalsIgnoreCase(".pdf")){
return Utils.TYPE_PDF;
}else if(type.equalsIgnoreCase(".txt")){
return Utils.TYPE_TXT;
}else{
for (String format : Utils.MIME_TYPE_PHOTO) {
if (format.equalsIgnoreCase(type)) {
return Utils.TYPE_PICTURE;
}
}
for (String format : Utils.MIME_TYPE_VIDEO_ALL) {
if (format.equalsIgnoreCase(type)) {
return Utils.TYPE_VIDEO;
}
}
for (String format : Utils.MIME_TYPE_MUSIC) {
if (format.equalsIgnoreCase(type)) {
return Utils.TYPE_AUDIO;
}
}
}
}else{
return Utils.TYPE_FILE;
}
return Utils.TYPE_FILE;
}
3.不同类型文件的Intent
/**对应文件的Intent*/
public static Intent getIntentByFileType(int type, String path){
switch(type){
case Utils.TYPE_AUDIO:
return getAudioFileIntent(path);
case Utils.TYPE_PICTURE:
return getImageFileIntent(path);
case Utils.TYPE_DOC:
return getWordFileIntent(path);
case Utils.TYPE_XLS:
return getExcelFileIntent(path);
case Utils.TYPE_PPT:
return getPptFileIntent(path);
case Utils.TYPE_PDF:
return getPdfFileIntent(path);
case Utils.TYPE_TXT:
return getTextFileIntent(path);
default:
return null;
}
}
/** android获取一个用于打开HTML文件的intent */
public static Intent getHtmlFileIntent(String param)
{
Uri uri = Uri.parse(param).buildUpon()
.encodedAuthority("com.android.htmlfileprovider")
.scheme("content").encodedPath(param).build();
Intent intent = new Intent("android.intent.action.VIEW");
intent.setDataAndType(uri, "text/html");
return intent;
}
/** android获取一个用于打开图片文件的intent */
public static Intent getImageFileIntent(String param)
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(new File(param));
intent.setDataAndType(uri, "image/*");
return intent;
}
/** android获取一个用于打开PDF文件的intent */
public static Intent getPdfFileIntent(String param)
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(new File(param));
intent.setDataAndType(uri, "application/pdf");
return intent;
}
/** android获取一个用于打开文本文件的intent */
public static Intent getTextFileIntent(String param
//,boolean paramBoolean)
)
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri1 = Uri.parse(param);
intent.setDataAndType(uri1, "text/plain");
return intent;
}
/** android获取一个用于打开音频文件的intent */
public static Intent getAudioFileIntent(String param)
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("oneshot", 0);
intent.putExtra("configchange", 0);
Uri uri = Uri.fromFile(new File(param));
intent.setDataAndType(uri, "audio/*");
return intent;
}
/** android获取一个用于打开视频文件的intent */
public static Intent getVideoFileIntent(String param)
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("oneshot", 0);
intent.putExtra("configchange", 0);
Uri uri = Uri.fromFile(new File(param));
intent.setDataAndType(uri, "video/*");
return intent;
}
/** android获取一个用于打开CHM文件的intent */
public static Intent getChmFileIntent(String param)
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(new File(param));
intent.setDataAndType(uri, "application/x-chm");
return intent;
}
/** android获取一个用于打开Word文件的intent */
public static Intent getWordFileIntent(String param)
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(new File(param));
intent.setDataAndType(uri, "application/msword");
return intent;
}
/** android获取一个用于打开Excel文件的intent */
public static Intent getExcelFileIntent(String param)
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(new File(param));
intent.setDataAndType(uri, "application/vnd.ms-excel");
return intent;
}
/** android获取一个用于打开PPT文件的intent */
public static Intent getPptFileIntent(String param)
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(new File(param));
intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
return intent;
}