import java.io.File;import java.util.HashMap;import android.content.Intent;import android.net.Uri;public class FileIntentUtil { private static final int INTENT_IMAGE = 1; private static final int INTENT_AUDIO = 2; private static final int INTENT_VIDEO = 3; private static final int INTENT_PACHAGE = 4; private static final int INTENT_WEBTEXT = 5; private static final int INTENT_TEXT = 6; private static final int INTENT_WORD = 7; private static final int INTENT_EXCEL = 8; private static final int INTENT_PPT = 9; private static final int INTENT_PDF = 10; private static final int INTENT_CHM = 11; private static final int INTENT_MAX = 12; private static FileIntentUtil _instance; private static Object lockObj = new Object(); private final HashMap<String, Integer> map; public Intent getFileIntent(String filepath) { return getFileIntent(new File(filepath)); } public Intent getFileIntent(File file) { String filename = file.getName(); String[] ss = filename.split("\\."); int type = -1; if (ss.length > 1) { String ext = ss[ss.length - 1]; if (ext != null && map.containsKey(ext)) { type = map.get(ext); } } if (type > 0 && type < INTENT_MAX) { Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(Intent.ACTION_VIEW); Uri uri = Uri.fromFile(file); switch (type) { case INTENT_AUDIO: intent.putExtra("oneshot", 0); intent.putExtra("configchange", 0); intent.setDataAndType(uri, "audio/*"); break; case INTENT_CHM: intent.setDataAndType(uri, "application/x-chm"); break; case INTENT_EXCEL: intent.setDataAndType(uri, "application/vnd.ms-excel"); break; case INTENT_IMAGE: intent.setDataAndType(uri, "image/*"); break; case INTENT_PDF: intent.setDataAndType(uri, "application/pdf"); break; case INTENT_PACHAGE: intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); break; case INTENT_PPT: intent.setDataAndType(uri, "application/vnd.ms-powerpoint"); break; case INTENT_TEXT: intent.setDataAndType(uri, "text/plain"); break; case INTENT_VIDEO: intent.putExtra("oneshot", 0); intent.putExtra("configchange", 0); intent.setDataAndType(uri, "video/*"); break; case INTENT_WEBTEXT: uri = Uri.fromFile(file).buildUpon() .encodedAuthority("com.android.htmlfileprovider") .scheme("content").encodedPath(file.toString()).build(); intent.setDataAndType(uri, "text/html"); break; case INTENT_WORD: intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType(uri, "application/msword"); break; } return intent; } return null; } private FileIntentUtil() { map = new HashMap<String, Integer>(); // images map.put("png", INTENT_IMAGE); map.put("gif", INTENT_IMAGE); map.put("jpg", INTENT_IMAGE); map.put("jpeg", INTENT_IMAGE); map.put("bmp", INTENT_IMAGE); // audio map.put("mp3", INTENT_AUDIO); map.put("wav", INTENT_AUDIO); map.put("ogg", INTENT_AUDIO); map.put("midi", INTENT_AUDIO); // video map.put("mp4", INTENT_VIDEO); map.put("rmvb", INTENT_VIDEO); map.put("avi", INTENT_VIDEO); map.put("flv", INTENT_VIDEO); // package map.put("jar", INTENT_PACHAGE); map.put("zip", INTENT_PACHAGE); map.put("rar", INTENT_PACHAGE); map.put("gz", INTENT_PACHAGE); map.put("apk", INTENT_PACHAGE); map.put("img", INTENT_PACHAGE); // web text map.put("htm", INTENT_WEBTEXT); map.put("html", INTENT_WEBTEXT); map.put("php", INTENT_WEBTEXT); map.put("jsp", INTENT_WEBTEXT); // text map.put("txt", INTENT_TEXT); map.put("java", INTENT_TEXT); map.put("c", INTENT_TEXT); map.put("cpp", INTENT_TEXT); map.put("py", INTENT_TEXT); map.put("xml", INTENT_TEXT); map.put("json", INTENT_TEXT); map.put("log", INTENT_TEXT); // word map.put("doc", INTENT_WORD); map.put("docx", INTENT_WORD); // excel map.put("xls", INTENT_EXCEL); map.put("xlsx", INTENT_EXCEL); // ppt map.put("ppt", INTENT_PPT); map.put("pptx", INTENT_PPT); // pdf map.put("pdf", INTENT_PDF); // chm map.put("chm", INTENT_CHM); } public static FileIntentUtil instance() { if (_instance == null) { synchronized (lockObj) { if (_instance == null) { _instance = new FileIntentUtil(); } } } return _instance; }}
详细解决方案
android 更具File 起动不同的intent
热度:18 发布时间:2016-04-28 01:02:19.0
相关解决方案
- IE8怎么让input file 的地址框可编辑
- File "/head.jsp" not found解决方法
- tomcat报错: File "/js_list.jsp" not found,该怎么解决
- 关于在struts2中怎么将.action加入到web.xml中的<welcome-file-list>上 急
- 关于<welcome-file-list>的疑问。初学者郁闷哦
- Response.AddHeader("Content-Disposition" "attachment; filename=" + file.Name+"xls");中文显示乱码解决方法
- jsp重命名上传的文件提示文件无法保存(File can't be saved (1120).)解决思路
- welcome-file 404的有关问题
- 文件抛java.io.IOException: at com.sun.midp.io.j2me.file.Protocol.delete错误
- android 读取byte[]中的元素解决方案
- android 标题栏兑现方式
- midlet.platformRequest("file:///.");打开本土Flash文件
- android 中Activity向BroadcastReceiver发送数据,该怎么解决
- Android 4.0 为什么模拟器老是提示小弟我谷歌拼音输入法已停止
- android:getSharedPreferences() 这是哪个类的方法解决思路
- android 怎么判断一个程序是否联网
- android 大量数据按周分组,该如何解决
- android RadioButton如何设置默认选中
- 關于new File(URI)路徑無法找到,该如何处理
- ksoap2-android-这个包,连接webService怎么设置超时
- android 怎么重新设置锚点
- android UI界面设计解决方案
- android 图片对象获取的有关问题
- java io操作,file stream变换
- <input type="file" name="filename">该如何处理
- android 怎么调用淘宝支付宝接口
- Android 沿袭InputMethodService自定义输入法
- android 关于服务连接的疑义
- android 两个activity如何通信
- java.io.eoefException bad file number串口收发错误