笔记-Android中打开各种格式的文件(apk、word、excel、ppt、pdf、音视频、图片等)

打开后缀.apk的文件,即启动安装程序;

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //apkFilePath 文件路径  
  2. ublic void installAPK(String apkFilePath) {             
  3. // 创建URI  
  4.        Uri uri = Uri.fromFile(new File(apkFilePath));  
  5.        Intent intent = new Intent(Intent.ACTION_VIEW);  
  6.        // 设置Uri和类型  
  7.        intent.setDataAndType(uri, "application/vnd.android.package-archive");  
  8.        // 执行安装  
  9.        mContext.startActivity(intent);  
  10.    }     
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  * 打开多种类型文件 
  3.  * @param path  文件路径 
  4.  * @param type  文件类型 
  5.  */  
  6. public void openText(String path , int type){  
  7.     Intent intent = new Intent(Intent.ACTION_VIEW);       
  8.        intent.addCategory(Intent.CATEGORY_DEFAULT);       
  9.        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);       
  10.        Uri uri = Uri.fromFile(new File(path ));   
  11.         //判断文件类型  
  12.        if (FILE_TYPE_PPT == type) {       
  13.            intent.setDataAndType(uri, "application/vnd.ms-powerpoint");    
  14.        } else if (FILE_TYPE_WORD == type) {  
  15.            intent.setDataAndType(uri, "application/msword");       
  16.        } else if(FILE_TYPE_EXCEL == type){  
  17.            intent.setDataAndType(uri, "application/vnd.ms-excel");       
  18.        } else if(FILE_TYPE_TXT == type){  
  19.            intent.setDataAndType(uri, "text/plain");       
  20.        } else if(FILE_TYPE_PDF == type){  
  21.            intent.setDataAndType(uri, "application/pdf");       
  22.        } else if(FILE_TYPE_HTML == type){  
  23.            Uri htmluri = Uri.parse(path).buildUpon().encodedAuthority("com.android.htmlfileprovider")  
  24.                    .scheme("content").encodedPath(path).build();    
  25.            intent.setDataAndType(htmluri, "text/html");   
  26.        }    
  27.        try {  
  28.        activity.startActivity(intent);  
  29.        } catch (Exception e) {  
  30.            Toast.makeText(mContext, "设备中没有安装支持该格式的程序", Toast.LENGTH_SHORT).show();  
  31.        }  
  32. }  
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <pre name="code" class="java">    ////////打开多媒体类型  
  2.     intent.setDataAndType(uri, "audio/*");  //音频  
  3.     intent.setDataAndType(uri, "video/*");  //视频  
  4.     intent.setDataAndType(uri, "image/*");  //图片  
  5.   
  6. intent.setDataAndType(uri, "application/x-chm");     //打开chm文件  



[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. ////判断文件名是否是某种类型的后缀  
  2.     private boolean check(final String name, final String[] extensions) {  
  3.         for (String end : extensions) {  
  4.               
  5.             if (name.toLowerCase().endsWith(end)) {  
  6.                 return true;  
  7.             }  
  8.         }  
  9.         return false;  
  10.     }  
  11.     /////////////设置类型         
  12.      if (check(name, ".apk")){     
  13.           file.setType(FILE_TYPE_APK);  
  14.         } else  if(check(name, ".pdf")){  
  15.              file.setType(FILE_TYPE_PDF);  
  16.         } else if(check(name,  
  17.                  getStringArray(R.array.ppt_filter))){  
  18.              file.setType(FILE_TYPE_PPT);  
  19.         }   
  20.                 ...................  
  21.     array.ppt_filter:  
  22.     <array name="ppt_filter">  
  23.         <item>.ppt</item>  
  24.         <item>.pptx</item>  
  25.         </array>  

//根据包名卸载apk

private void uninstallPkg(String pkg) {
    Uri packageURI = Uri.parse("package:"+pkg);   
    Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);   
    startActivity(uninstallIntent);

//也可以用这种方法卸载

// getPackageManager().deletePackage(pkg, null, 0); 
   }


//获取设备存储路径(sd卡,usb)
Environment.getExternalStorageDirectory();

Environment.getExternalStorageDirectory().getParent();

posted on 2016-03-03 18:40  猪猪一号  阅读(979)  评论(0编辑  收藏  举报

导航