笔记-Android中打开各种格式的文件(apk、word、excel、ppt、pdf、音视频、图片等)
2014-06-18 10:36
851 查看
打开后缀.apk的文件,即启动安装程序;
//根据包名卸载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();
//apkFilePath 文件路径 public void installAPK(String apkFilePath) { // 创建URI Uri uri = Uri.fromFile(new File(apkFilePath)); Intent intent = new Intent(Intent.ACTION_VIEW); // 设置Uri和类型 intent.setDataAndType(uri, "application/vnd.android.package-archive"); // 执行安装 mContext.startActivity(intent); }
/** * 打开多种类型文件 * @param path 文件路径 * @param type 文件类型 */ public void openText(String path , int type){ Intent intent = new Intent(Intent.ACTION_VIEW); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Uri uri = Uri.fromFile(new File(path )); //判断文件类型 if (FILE_TYPE_PPT == type) { intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); } else if (FILE_TYPE_WORD == type) { intent.setDataAndType(uri, "application/msword"); } else if(FILE_TYPE_EXCEL == type){ intent.setDataAndType(uri, "application/vnd.ms-excel"); } else if(FILE_TYPE_TXT == type){ intent.setDataAndType(uri, "text/plain"); } else if(FILE_TYPE_PDF == type){ intent.setDataAndType(uri, "application/pdf"); } else if(FILE_TYPE_HTML == type){ Uri htmluri = Uri.parse(path).buildUpon().encodedAuthority("com.android.htmlfileprovider") .scheme("content").encodedPath(path).build(); intent.setDataAndType(htmluri, "text/html"); } try { activity.startActivity(intent); } catch (Exception e) { Toast.makeText(mContext, "设备中没有安装支持该格式的程序", Toast.LENGTH_SHORT).show(); } }
<pre name="code" class="java"> ////////打开多媒体类型 intent.setDataAndType(uri, "audio/*"); //音频 intent.setDataAndType(uri, "video/*"); //视频 intent.setDataAndType(uri, "image/*"); //图片 intent.setDataAndType(uri, "application/x-chm"); //打开chm文件
////判断文件名是否是某种类型的后缀 private boolean check(final String name, final String[] extensions) { for (String end : extensions) { if (name.toLowerCase().endsWith(end)) { return true; } } return false; } /////////////设置类型 if (check(name, ".apk")){ file.setType(FILE_TYPE_APK); } else if(check(name, ".pdf")){ file.setType(FILE_TYPE_PDF); } else if(check(name, getStringArray(R.array.ppt_filter))){ file.setType(FILE_TYPE_PPT); } ................... array.ppt_filter: <array name="ppt_filter"> <item>.ppt</item> <item>.pptx</item> </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();
相关文章推荐
- 笔记-Android中打开各种格式的文件(apk、word、excel、ppt、pdf、音视频、图片等)
- Android中打开各种格式的文件(apk、word、excel、ppt、pdf、音视频、图片等)
- android用于打开各种文件的intent,包括以下文件PDF,PPT,WORD,EXCEL,CHM,HTML,TEXT,AUDIO,VIDEO
- Android遍历获取Office格式(Word,Excel,PPT,PDF)的文件并打开
- android用于打开各种文件的intent,包括以下文件PDF,PPT,WORD,EXCEL,CHM,HTML,TEXT,AUDIO,VIDE(代码待完善)
- android Intent打开各种类型文件((PDF、word、excel、ppt、chm)
- android Intent打开各种类型文件(PDF、word、excel、ppt、chm)
- android Intent打开各种类型文件((PDF、word、excel、ppt、chm)
- Android遍历获取Office格式(Word,Excel,PPT,PDF)的文件并打开
- android Intent 默认打开打开各种类型文件((PDF、word、excel、ppt、chm)
- android用于打开各种文件的intent,包括以下文件PDF,PPT,WORD,EXCEL,CHM,HTML,TEXT,AUDIO,VIDEO
- Android遍历获取Office格式(Word,Excel,PPT,PDF)的文件并打开
- android Intent打开各种类型文件((PDF、word、excel、ppt、chm)
- android Intent打开各种类型文件(PDF,PPT,WORD,EXCEL,CHM,H...
- android Intent打开各种类型文件((PDF、word、excel、ppt、chm)
- android打开pdf ppt word excel等文件
- IOS 打开预览pdf、ppt、excel、word、tet、rtf、csv格式的文件,微信文件分享
- android打开pdf ppt word excel chm html text 文件的intent
- Android执行打开文件(PDF,PPT,WORD,EXCEL,CHM,HTML,TEXT,AUDIO,VIDEO)的总结
- android 开发 获取各种intent (图片、apk文件、excel、pdf等文件)