Intent(open file) android
16lz
2022-05-20
这周我就写写如何在android上打开各种文件的方法(前提:你安装了相应文件的阅读器,eg:pdf)
step1:我们将文件的后缀和相应的datatype存入hashmap中,例如:
HashMap<String , String> mTypeMap = new HashMap<String, String>();mTypeMap.put("pdf", "application/pdf");mTypeMap.put("rar", "application/rar");mTypeMap.put("doc", "application/msword");mTypeMap.put("mp3", "audio/mpeg");
step2:根据文件名获取文件后缀:eg. pdf
step3:根据后缀名获取datatype:eg. pdf ---------->application/pdf,假设其方法名为:getMineType(String name)
step4:new Intent,并将文件的路径传递给准备跳转的activity:
String action = "android.intent.action.VIEW";Intent intent = new Intent();intent.setAction(action);intent.setDataAndType(uri, getMineType("pdf"));startActivity(intent);
注:以下是一些后缀对应的datatype,可以参考参考:
"application/ogg", "ogg""application/pdf", "pdf""application/rar", "rar""application/rdf+xml", "rdf""application/rss+xml", "rss""application/zip", "zip""application/vnd.android.package-archive", "apk""application/vnd.oasis.opendocument.database", "odb""application/vnd.oasis.opendocument.formula", "odf""application/vnd.oasis.opendocument.graphics", "odg""application/vnd.oasis.opendocument.spreadsheet", "ods""application/vnd.oasis.opendocument.spreadsheet-template","ots""application/vnd.oasis.opendocument.text", "odt""application/vnd.oasis.opendocument.text-master", "odm""application/msword", "doc""application/msword", "dot""application/vnd.openxmlformats-officedocument.wordprocessingml.document","docx""application/vnd.ms-excel", "xls""application/vnd.openxmlformats-officedocument.spreadsheetml.sheet","xlsx""application/vnd.ms-powerpoint", "ppt""application/vnd.openxmlformats-officedocument.presentationml.presentation","pptx");"application/vnd.visio", "vsd""audio/3gpp", "3gpp" "audio/mpeg", "mp3" "audio/x-ms-wma", "wma" "audio/x-pn-realaudio", "rm" "image/bmp", "bmp" "image/gif", "gif" "image/ico", "cur""image/ico", "ico" "image/x-ms-bmp", "bmp" "text/css", "css" "text/html", "htm""text/html", "html" "text/plain", "txt" "text/plain", "text""text/xml", "xml""text/x-c++src", "c++""text/x-c++src", "cpp""text/x-component", "htc""text/x-csh", "csh""text/x-csrc", "c""text/x-dsrc", "d""text/x-haskell", "hs""text/x-java", "java""text/x-literate-haskell", "lhs""text/x-moc", "moc""text/x-pascal", "p""text/x-pascal", "pas""text/x-pcs-gcd", "gcd""text/x-setext", "etx""text/x-tcl", "tcl""text/x-tex", "tex""text/x-tex", "ltx""text/x-tex", "sty""text/x-tex", "cls""text/x-vcalendar", "vcs");"text/x-vcard", "vcf");"video/3gpp", "3gpp""video/3gpp", "3gp""video/3gpp", "3g2""video/dl", "dl""video/dv", "dif""video/dv", "dv""video/fli", "fli""video/m4v", "m4v""video/mpeg", "mpeg""video/mpeg", "mpg""video/mpeg", "mpe""video/mp4", "mp4""video/mpeg", "vob""video/quicktime", "qt""video/quicktime", "mov""video/vnd.mpegurl", "mxu""video/x-la-asf", "lsf""video/x-la-asf", "lsx""video/x-mng", "mng""video/x-ms-asf", "asf""video/x-ms-asf", "asx""video/x-ms-wm", "wm""video/x-ms-wmv", "wmv""video/x-ms-wmx", "wmx""video/x-ms-wvx", "wvx""video/x-msvideo", "avi""video/x-sgi-movie", "movie""x-conference/x-cooltalk", "ice""x-epoc/x-sisx-app", "sisx"
更多相关文章
- 导入Android工程遇到的问题
- Android(安卓)zipalign: error while loading shared libraries:
- Android(安卓)Studio NDK及so文件开发 以及常见错误
- Android上传图片到服务器,android-async-http上传图片到服务器
- Android从远程服务器下载文件到本地sd卡中
- 小白学习android(一):android开发常识问题
- Android中WebView获取网页中标题 ,内容, 图片的方法
- NPM 和webpack 的基础使用
- 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程