Android 打开本地文件

Android 打开本地的文件,目前来说,其实很常见。而且现在有手机版的office了。查看office的全家桶就更加方便。

首先要知道的是,Android 打开本地文件是根据类型打开的,也就是根据文件的 MIME 类型来确定

如果不知道是什么类型,那就是 : */*

类型匹配表:

private static final String[][] MIME_MapTable={
            //{后缀名,    MIME类型}
            {".3gp",    "video/3gpp"},
            {".apk",    "application/vnd.android.package-archive"},
            {".asf",    "video/x-ms-asf"},
            {".avi",    "video/x-msvideo"},
            {".bin",    "application/octet-stream"},
            {".bmp",      "image/bmp"},
            {".c",        "text/plain"},
            {".class",    "application/octet-stream"},
            {".conf",    "text/plain"},
            {".cpp",    "text/plain"},
            {".doc",    "application/msword"},
            {".exe",    "application/octet-stream"},
            {".gif",    "image/gif"},
            {".gtar",    "application/x-gtar"},
            {".gz",        "application/x-gzip"},
            {".h",        "text/plain"},
            {".htm",    "text/html"},
            {".html",    "text/html"},
            {".jar",    "application/java-archive"},
            {".java",    "text/plain"},
            {".jpeg",    "image/jpeg"},
            {".jpg",    "image/jpeg"},
            {".js",        "application/x-javascript"},
            {".log",    "text/plain"},
            {".m3u",    "audio/x-mpegurl"},
            {".m4a",    "audio/mp4a-latm"},
            {".m4b",    "audio/mp4a-latm"},
            {".m4p",    "audio/mp4a-latm"},
            {".m4u",    "video/vnd.mpegurl"},
            {".m4v",    "video/x-m4v"},
            {".mov",    "video/quicktime"},
            {".mp2",    "audio/x-mpeg"},
            {".mp3",    "audio/x-mpeg"},
            {".mp4",    "video/mp4"},
            {".mpc",    "application/vnd.mpohun.certificate"},
            {".mpe",    "video/mpeg"},
            {".mpeg",    "video/mpeg"},
            {".mpg",    "video/mpeg"},
            {".mpg4",    "video/mp4"},
            {".mpga",    "audio/mpeg"},
            {".msg",    "application/vnd.ms-outlook"},
            {".ogg",    "audio/ogg"},
            {".pdf",    "application/pdf"},
            {".png",    "image/png"},
            {".pps",    "application/vnd.ms-powerpoint"},
            {".ppt",    "application/vnd.ms-powerpoint"},
            {".prop",    "text/plain"},
            {".rar",    "application/x-rar-compressed"},
            {".rc",        "text/plain"},
            {".rmvb",    "audio/x-pn-realaudio"},
            {".rtf",    "application/rtf"},
            {".sh",        "text/plain"},
            {".tar",    "application/x-tar"},
            {".tgz",    "application/x-compressed"},
            {".txt",    "text/plain"},
            {".wav",    "audio/x-wav"},
            {".wma",    "audio/x-ms-wma"},
            {".wmv",    "audio/x-ms-wmv"},
            {".wps",    "application/vnd.ms-works"},
            //{".xml",    "text/xml"},
            {".xml",    "text/plain"},
            {".z",        "application/x-compress"},
            {".zip",    "application/zip"},
            {"",        "*/*"}
    };

  获取文件的类型。

    private String getMIMEType(File file) {

        String type="*/*";
        String fName = file.getName();
        //获取后缀名前的分隔符"."在fName中的位置。
        int dotIndex = fName.lastIndexOf(".");
        if(dotIndex < 0)
            return type;
        /* 获取文件的后缀名 */
        String fileType = fName.substring(dotIndex,fName.length()).toLowerCase();
        if(fileType == null || "".equals(fileType))
            return type;
        //在MIME和文件类型的匹配表中找到对应的MIME类型。
        for(int i=0;i<MIME_MapTable.length;i++){
            if(fileType.equals(MIME_MapTable[i][0]))
                type = MIME_MapTable[i][1];
        }
        return type;
    }

  打开文件

    Intent intent = new Intent();
    File file = new File(FileDirectory);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//设置标记
    intent.setAction(Intent.ACTION_VIEW);//动作,查看
    intent.setDataAndType(Uri.fromFile(file), getMIMEType(file));//设置类型
    context.startActivity(intent);

  

时间: 2024-08-04 05:58:59

Android 打开本地文件的相关文章

Android -- 打开本地图片且显示路径

背景                                                                                          代码                                                                                           先上布局文件: <LinearLayout xmlns:android="http://schemas.android.co

android webview处理h5打开本地文件浏览器的功能

这周遇到一个比较棘手的问题,需要在android上边集成h5页面,并且在h5页面上,需要用户能够上传android本地的照片,一开始我以为webview会自动处理掉的,因此没太留意,当真正集成时,才发现,h5界面上传图片无法打开本地android的图库,h5调用的方式是: <input type = "file"/> 通过最为简单的input菜单来选择,于是我就百度了一波,找到了两种比较好的解决方法,一种是h5编写js代码,调用android app实现的函数,来实现打开图

android -------- 打开本地浏览器或指定浏览器加载,打电话,打开第三方app

开发中常常有打开本地浏览器加载url或者指定浏览器加载, 还有打开第三方app, 如 打开高德地图 百度地图等 在Android程序中我们可以通过发送隐式Intent来启动系统默认的浏览器. 如果手机本身安装了多个浏览器而又没有设置默认浏览器的话,系统将让用户选择使用哪个浏览器来打开连接. Uri uri = Uri.parse("https://www.baidu.com"); Intent intent = new Intent(Intent.ACTION_VIEW, uri);

Google调用explorer.exe打开本地文件

给IE浏览器地址栏输个本地文件路径,会自动用explorer.exe打开,这个挺好的,但是IE对jQuery稍微高点的版本不怎么待见,只好自己给Google折腾一个调用explorer的功能------ 1.自定义URL Protocol 协议,让浏览器可以启动本地程序 2.编写c++控制台程序:解码从浏览器传递过来的url(url===utf-8===Unicode===gb2312)-->将链接路径头部去掉-->替换"|"为"\\"(解码的时候会把u

用默认的打开方式打开本地文件

ShellExecute( hWnd: HWND; {指定父窗口句柄} Operation: PChar; {指定动作, 譬如: open.print} FileName: PChar; {指定要打开的文件或程序} Parameters: PChar; {给要打开的程序指定参数; 如果打开的是文件这里应该是 nil}     Directory: PChar; {缺省目录} ShowCmd: Integer {打开选项}): #include <Windows.h> ShellExecuteA

android 打开各种文件(setDataAndType)

Java代码  /** * 打开文件 * @param 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(

mfc打开本地文件夹

使用ShellExecute来执行open操作 例如要打开D盘下的Test文件夹,如下 ShellExecute(NULL,"open","D:\\Tset",NULL,NULL,SW_SHOWNORMAL);

Qt打开外部程序和文件夹需要注意的细节(注意QProcess的空格问题,以及打开本地文件时,需要QUrl::fromLocalFile才可以)

下午写程序中遇到几个小细节,需要在这里记录一下. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 QProcess *process = new QProcess(this);     QFileInfo fileinfo(appUrl);     QString appPath = QApplication::applicationDirPath()+SAVEDIR+"/"+fileinfo.fileName();     bool res = proce

js实现打开本地文件或文件夹

原网址:http://blog.csdn.net/cofesun/article/details/7904887javascript有个特殊的对象ActiveXObject,通过它可以访问windows的本地文件系统和应用程序 <script> function openFileIIs(filename){ try{ var obj=new ActiveXObject("wscript.shell"); if(obj){ obj.Run("\""