//File指的是文件路径

      private void openFile(File file){ 

      Intent intent = new Intent(); 
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     //设置intent的Action属性 
    intent.setAction(Intent.ACTION_VIEW); 
    //获取文件file的MIME类型 
    String type = getMIMEType(file); 
    //设置intent的data和Type属性。 
    intent.setDataAndType(Uri.fromFile(file), type); 
    //跳转 
    startActivity(intent);   
     

 

//判断文件MimeType的方法

private String getMimeType(File f){
  String type="";

String fName = f.getName();

//取得扩展名

String end = fName.substring(fName.lastIndexOf(".")+1 , fName.length()).toLowerCase());

  //根据扩展名决定Mime类型

if(end.equals("m4a") || end.equals("mp3") || end.equals("mid") || end.equals("xmf") || end.equals("ogg") || end.equals("wav")){

  type = "audio";
 

else if(end.equals("3gp") || end.equals("mp4")){
  type = "video";

}

else if(end.equals("jpg") || end.equals("gif") || end.equals("png") || end.equals("jpeg") || end.equals("bmp")){

type = "image";   

else if(end.equals("apk")){
  //打开安装apk程序 , 需要在AndroidManifest中注册 android.permission.INSTALL_PACKAGES

type = "application/vnd.android.package-archive";

}

return type; 

}

相关文章:

  • 2021-10-19
  • 2022-01-05
  • 2021-04-14
  • 2022-02-21
  • 2021-07-01
  • 2021-06-08
猜你喜欢
  • 2022-12-23
  • 2021-04-02
  • 2022-12-23
  • 2022-12-23
  • 2021-11-29
  • 2021-12-13
相关资源
相似解决方案