zoukankan      html  css  js  c++  java
  • Android之在手机上打开文件的方法

       //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; 

    }

  • 相关阅读:
    点赞
    js点击事件,数字累加
    html中hr的各种样式使用
    基于Bootstrap垂直响应的jQuery时间轴特效
    bootstrop日历
    前端经验
    bootstrop登陆页面
    bootstrop设置背景图片自适应屏幕
    建立博客的第一天
    php伪静态--隐藏地址实际路径方法
  • 原文地址:https://www.cnblogs.com/lee0oo0/p/2543574.html
Copyright © 2011-2022 走看看