android 根据文件的扩展名选择用什么应用程序打开

private void openFile(File f)  
 {  
   Intent intent = new Intent();  
   intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
   intent.setAction(android.content.Intent.ACTION_VIEW);  
   String type = getMIMEType(f);  
   intent.setDataAndType(Uri.fromFile(f), type);  
   startActivity(intent);  
 }  
 
 private String getMIMEType(File f){  
   String end = f  
       .getName()  
       .substring(f.getName().lastIndexOf(".") + 1,  
           f.getName().length()).toLowerCase();  
   String type = "";  
   if (end.equals("mp3") || end.equals("aac") || end.equals("aac")  
       || end.equals("amr") || end.equals("mpeg")  
       || end.equals("mp4"))  
   {  
     type = "audio";  
   } else if (end.equals("jpg") || end.equals("gif")  
       || end.equals("png") || end.equals("jpeg"))  
   {  
     type = "image";  
   } else  
   {  
     type = "*";  
   }  
   type += "/*";  
   return type;  
 } 

你可能感兴趣的:(android,String,image,File,扩展,audio)