美文网首页今日看点程序员
Android 关于地图的一些细节

Android 关于地图的一些细节

作者: 黄海佳 | 来源:发表于2017-08-26 11:10 被阅读543次

    有时候我们需要调用地图显示一下位置,这时候可能还需要导航

    1、判断手机上有没有安装各种地图
     /**
         * @描述 检查手机上是否安装了地图
         * @return
         */
        public static boolean appHasMap(Context context) {
            
            if (isAvilible(context, "com.baidu.BaiduMap") || isAvilible(context, "com.tencent.map")
                    || isAvilible(context, "com.autonavi.minimap")
                    || isAvilible(context, "com.google.android.apps.maps")
                    || isAvilible(context, "cld.navi.mainframe")
                    || isAvilible(context, "com.sogou.map.android.maps")) {
                return true;
            } 
            return false;
            
        }
    

    另外有一种办法是通过循环包名判断系统是否有包

    /**
    * 检查手机上是否安装了指定的软件 
    * @param context 
    * @param packageName:应用包名 
     * @return 
     */  
    public static boolean isAvilible(Context context, String packageName){   
        //获取packagemanager   
        final PackageManager packageManager = context.getPackageManager();  
        //获取所有已安装程序的包信息   
        List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);  
        //用于存储所有已安装程序的包名   
        List<String> packageNames = new ArrayList<String>();  
        //从pinfo中将包名字逐一取出,压入pName list中   
        if(packageInfos != null){   
           for(int i = 0; i < packageInfos.size(); i++){   
               String packName = packageInfos.get(i).packageName;   
               packageNames.add(packName);   
           }   
         }   
        //判断packageNames中是否有目标程序的包名,有TRUE,没有FALSE   
        return packageNames.contains(packageName);  
    }   
    
    
    2、各种应用打开的列子:

    如百度:

    if(isAvilible(context,"com.baidu.BaiduMap")){//传入指定应用包名  
    
            try {
               //intent = Intent.getIntent("intent://map/direction?origin=latlng:34.264642646862,108.95108518068|name:我家&destination=大雁塔&mode=driving®ion=西安&src=yourCompanyName|yourAppName#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end");  
                intent = Intent.getIntent("intent://map/direction?" +
                        //"origin=latlng:"+"34.264642646862,108.95108518068&" +   //起点  此处不传值默认选择当前位置
                        "destination=latlng:"+location[0]+","+location[1]+"|name:我的目的地"+        //终点
                        "&mode=driving&" +          //导航路线方式
                        "region=北京" +           //
                        "&src=慧医#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end");
                context.startActivity(intent); //启动调用  
            } catch (URISyntaxException e) {
                Log.e("intent", e.getMessage());
            }
        }else{//未安装  
            //market为路径,id为包名  
            //显示手机上所有的market商店  
            Toast.makeText(context, "您尚未安装百度地图", Toast.LENGTH_LONG).show();
            Uri uri = Uri.parse("market://details?id=com.baidu.BaiduMap");
            intent = new Intent(Intent.ACTION_VIEW, uri);
            context.startActivity(intent);
        }
    
    

    如高德

    if (isAvilible(context, "com.autonavi.minimap")) {
            try{
                intent = Intent.getIntent("androidamap://navi?sourceApplication=慧医&poiname=我的目的地&lat="+location[0]+"&lon="+location[1]+"&dev=0");
                context.startActivity(intent);
            } catch (URISyntaxException e)
            {e.printStackTrace(); }
        }else{
            Toast.makeText(context, "您尚未安装高德地图", Toast.LENGTH_LONG).show();
            Uri uri = Uri.parse("market://details?id=com.autonavi.minimap");
            intent = new Intent(Intent.ACTION_VIEW, uri);
            context.startActivity(intent);
        }
    

    谷歌地图

    if (isAvilible(context,"com.google.android.apps.maps")) {
            Uri gmmIntentUri = Uri.parse("google.navigation:q="+location[0]+","+location[1] +", + Sydney +Australia");
            Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
            mapIntent.setPackage("com.google.android.apps.maps");
            context.startActivity(mapIntent);
        }else {
            Toast.makeText(context, "您尚未安装谷歌地图", Toast.LENGTH_LONG).show();
    
            Uri uri = Uri.parse("market://details?id=com.google.android.apps.maps");
            intent = new Intent(Intent.ACTION_VIEW, uri);
            context.startActivity(intent);  
        }
    
    3、网页打开地图

    如百度

    private void openBrowserBaidu() {  
        Uri webpage = Uri.parse("http://api.map.baidu.com/marker?location=39.915168,116.403875&title=目的地&content=天安门&output=html");  
        Intent webIntent = new Intent(Intent.ACTION_VIEW,webpage);  
        startActivity(webIntent);  
    }  
    
    4、各个地图的网站

    百度地图:http://lbsyun.baidu.com/index.php?title=uri/api/android
    高德地图:http://lbs.amap.com/api/uri-api/android-uri-explain/
    腾讯地图:http://lbs.qq.com/uri_v1/index.html
    谷歌地图:https://developers.google.com/maps/documentation/android-api/intents

    5、调用系统geo-uri方式调用外部程序方法来打开地图(不建议使用)
    //geo:latitude,longitude
    //geo:latitude,longitude?z=zoom,z表示zoom级别,值为数字1到23
    //geo:0,0?q=my+street+address
    //geo:0,0?q=business+near+city
    Uri mUri = Uri.parse("geo:39.940409,116.355257?q=西直门");
    Intent mIntent = new Intent(Intent.ACTION_VIEW,mUri);
    startActivity(mIntent);
    

    这段代码将会弹出一个对话框,显示所有在initer-filter中注册了geo-uri类型的程序,让用户进行选择,如果我们的程序也需要支持处理geo-uri,可以通过在AndroidMainfest文件中添加如下代码来实现:

    <intent-filter android:priority="0" >
          <action android:name="android.intent.action.VIEW" />
          <category android:name="android.intent.category.DEFAULT" />
          <category android:name="android.intent.category.BROWSABLE" />
          <data android:scheme="geo" />
    </intent-filter>
    

    但是使用AS3.0进行打包的时候会提示initer-filter要放进<activity>标签内,无法打包成功。
    但是如果你把这个方法放进你的地图里面,它会默认把你的那个模块也列进去,所以有bug。而且百度地图、高德地图的坐标位置都是不对应,需要转换的,所以此办法如果是传经纬度导航可能会偏差。

    6、其他各种地图坐标转工具类(已测过,跟IOS的转换结果一模一样)
    /**
     * @创建 HaiJia
     * @时间 2017/8/29 16:42
     * @描述
     *      各地图API坐标系统比较与转换;
     *      WGS84坐标系:即地球坐标系,国际上通用的坐标系。设备一般包含GPS芯片或者北斗芯片获取的经纬度为WGS84地理坐标系,
     *      谷歌地图采用的是WGS84地理坐标系(中国范围除外);
     *      GCJ02坐标系:即火星坐标系,是由中国国家测绘局制订的地理信息系统的坐标系统。由WGS84坐标系经加密后的坐标系。
     *      谷歌中国地图和搜搜中国地图采用的是GCJ02地理坐标系; BD09坐标系:即百度坐标系,GCJ02坐标系经加密后的坐标系;
     *      搜狗坐标系、图吧坐标系等,估计也是在GCJ02基础上加密而成的。
     */
    public class PositionUtil {
    
    
        public static final String BAIDU_LBS_TYPE = "bd09ll";
    
        public static double pi = 3.1415926535897932384626;
        public static double a = 6378245.0;
        public static double ee = 0.00669342162296594323;
    
        /**
         * 84 to 火星坐标系 (GCJ-02) World Geodetic System ==> Mars Geodetic System
         *
         * @param lat
         * @param lon
         * @return
         */
        public static Gps gps84_To_Gcj02(double lat, double lon) {
            if (outOfChina(lat, lon)) {
                return null;
            }
            double dLat = transformLat(lon - 105.0, lat - 35.0);
            double dLon = transformLon(lon - 105.0, lat - 35.0);
            double radLat = lat / 180.0 * pi;
            double magic = Math.sin(radLat);
            magic = 1 - ee * magic * magic;
            double sqrtMagic = Math.sqrt(magic);
            dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
            dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
            double mgLat = lat + dLat;
            double mgLon = lon + dLon;
            return new Gps(mgLat, mgLon);
        }
    
        /**
         * * 火星坐标系 (GCJ-02) to 84 * * @param lon * @param lat * @return
         * */
        public static Gps gcj_To_Gps84(double lat, double lon) {
            Gps gps = transform(lat, lon);
            double lontitude = lon * 2 - gps.getWgLon();
            double latitude = lat * 2 - gps.getWgLat();
            return new Gps(latitude, lontitude);
        }
    
        /**
         * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换算法 将 GCJ-02 坐标转换成 BD-09 坐标
         *
         * @param gg_lat
         * @param gg_lon
         */
        public static Gps gcj02_To_Bd09(double gg_lat, double gg_lon) {
            double x = gg_lon, y = gg_lat;
            double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * pi);
            double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * pi);
            double bd_lon = z * Math.cos(theta) + 0.0065;
            double bd_lat = z * Math.sin(theta) + 0.006;
            return new Gps(bd_lat, bd_lon);
        }
    
        /**
         * * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换算法 * * 将 BD-09 坐标转换成GCJ-02 坐标 * * @param
         * bd_lat * @param bd_lon * @return
         */
        public static Gps bd09_To_Gcj02(double bd_lat, double bd_lon) {
            double x = bd_lon - 0.0065, y = bd_lat - 0.006;
            double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * pi);
            double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * pi);
            double gg_lon = z * Math.cos(theta);
            double gg_lat = z * Math.sin(theta);
            return new Gps(gg_lat, gg_lon);
        }
    
        /**
         * (BD-09)-->84
         * @param bd_lat
         * @param bd_lon
         * @return
         */
        public static Gps bd09_To_Gps84(double bd_lat, double bd_lon) {
    
            Gps gcj02 = PositionUtil.bd09_To_Gcj02(bd_lat, bd_lon);
            Gps map84 = PositionUtil.gcj_To_Gps84(gcj02.getWgLat(),
                    gcj02.getWgLon());
            return map84;
    
        }
    
        public static boolean outOfChina(double lat, double lon) {
            if (lon < 72.004 || lon > 137.8347)
                return true;
            if (lat < 0.8293 || lat > 55.8271)
                return true;
            return false;
        }
    
        public static Gps transform(double lat, double lon) {
            if (outOfChina(lat, lon)) {
                return new Gps(lat, lon);
            }
            double dLat = transformLat(lon - 105.0, lat - 35.0);
            double dLon = transformLon(lon - 105.0, lat - 35.0);
            double radLat = lat / 180.0 * pi;
            double magic = Math.sin(radLat);
            magic = 1 - ee * magic * magic;
            double sqrtMagic = Math.sqrt(magic);
            dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
            dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
            double mgLat = lat + dLat;
            double mgLon = lon + dLon;
            return new Gps(mgLat, mgLon);
        }
    
        public static double transformLat(double x, double y) {
            double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y
                    + 0.2 * Math.sqrt(Math.abs(x));
            ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
            ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
            ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
            return ret;
        }
    
        public static double transformLon(double x, double y) {
            double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1
                    * Math.sqrt(Math.abs(x));
            ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
            ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
            ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0
                    * pi)) * 2.0 / 3.0;
            return ret;
        }
    
        public static void main(String[] args) {
    
            // 北斗芯片获取的经纬度为WGS84地理坐标 31.426896,119.496145
            Gps gps = new Gps(31.426896, 119.496145);
            System.out.println("gps :" + gps);
            Gps gcj = gps84_To_Gcj02(gps.getWgLat(), gps.getWgLon());
            System.out.println("gcj :" + gcj);
            Gps star = gcj_To_Gps84(gcj.getWgLat(), gcj.getWgLon());
            System.out.println("star:" + star);
            Gps bd = gcj02_To_Bd09(gcj.getWgLat(), gcj.getWgLon());
            System.out.println("bd  :" + bd);
            Gps gcj2 = bd09_To_Gcj02(bd.getWgLat(), bd.getWgLon());
            System.out.println("gcj :" + gcj2);
        }
    }
    
    

    另外Gps 对象如下

    /**
     * @创建 HaiJia
     * @时间 2017/8/29 16:41
     * @描述 Gps 对象
     */
    
    public class Gps {
        private double wgLat;
        private double wgLon;
        public Gps(double wgLat, double wgLon) {
            setWgLat(wgLat);
            setWgLon(wgLon);
        }
        public double getWgLat() {
            return wgLat;
        }
        public void setWgLat(double wgLat) {
            this.wgLat = wgLat;
        }
        public double getWgLon() {
            return wgLon;
        }
        public void setWgLon(double wgLon) {
            this.wgLon = wgLon;
        }
        @Override
        public String toString() {
            return wgLat + "," + wgLon;
        }
    }
    
    7、地理坐标的一些知识普及
    /**
     *
     *  API                坐标系
     *  CLLocationManager  地球坐标 (WGS84)
     *  Google 卫星地图     地球坐标  (WGS84)
    
     *  百度地图API         百度坐标(BD-09)
    
     *  腾讯搜搜地图API      火星坐标(GCJ-02)
     *  iOS地图            火星坐标(GCJ-02)
     *  阿里云地图API       火星坐标(GCJ-02)
     *  高德MapABC地图API   火星坐标(GCJ-02)
     *  灵图51ditu地图API   火星坐标(GCJ-02)
     *  @brief  世界标准地理坐标(WGS-84) 转换成 中国国测局地理坐标(GCJ-02)<火星坐标>
     *
     *  ####只在中国大陆的范围的坐标有效,以外直接返回世界标准坐标
     *
     *  location    世界标准地理坐标(WGS-84)
    **/
    

    8、另外附上前端的地球坐标转火星坐标的JS(公司在用的)

    var GPS = {
        PI : 3.14159265358979324,
        x_pi : 3.14159265358979324 * 3000.0 / 180.0,
        delta : function (lat, lon) {
            // Krasovsky 1940
            //
            // a = 6378245.0, 1/f = 298.3
            // b = a * (1 - f)
            // ee = (a^2 - b^2) / a^2;
            var a = 6378245.0; //  a: 卫星椭球坐标投影到平面地图坐标系的投影因子。
            var ee = 0.00669342162296594323; //  ee: 椭球的偏心率。
            var dLat = this.transformLat(lon - 105.0, lat - 35.0);
            var dLon = this.transformLon(lon - 105.0, lat - 35.0);
            var radLat = lat / 180.0 * this.PI;
            var magic = Math.sin(radLat);
            magic = 1 - ee * magic * magic;
            var sqrtMagic = Math.sqrt(magic);
            dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * this.PI);
            dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * this.PI);
            return {'lat': dLat, 'lon': dLon};
        },
         
        //WGS-84 to GCJ-02
        gcj_encrypt : function (wgsLat, wgsLon) {
            if (this.outOfChina(wgsLat, wgsLon))
                return {'lat': wgsLat, 'lon': wgsLon};
     
            var d = this.delta(wgsLat, wgsLon);
            return {'lat' : wgsLat + d.lat,'lon' : wgsLon + d.lon};
        },
        //GCJ-02 to WGS-84
        gcj_decrypt : function (gcjLat, gcjLon) {
            if (this.outOfChina(gcjLat, gcjLon))
                return {'lat': gcjLat, 'lon': gcjLon};
             
            var d = this.delta(gcjLat, gcjLon);
            return {'lat': gcjLat - d.lat, 'lon': gcjLon - d.lon};
        },
        //GCJ-02 to WGS-84 exactly
        gcj_decrypt_exact : function (gcjLat, gcjLon) {
            var initDelta = 0.01;
            var threshold = 0.000000001;
            var dLat = initDelta, dLon = initDelta;
            var mLat = gcjLat - dLat, mLon = gcjLon - dLon;
            var pLat = gcjLat + dLat, pLon = gcjLon + dLon;
            var wgsLat, wgsLon, i = 0;
            while (1) {
                wgsLat = (mLat + pLat) / 2;
                wgsLon = (mLon + pLon) / 2;
                var tmp = this.gcj_encrypt(wgsLat, wgsLon)
                dLat = tmp.lat - gcjLat;
                dLon = tmp.lon - gcjLon;
                if ((Math.abs(dLat) < threshold) && (Math.abs(dLon) < threshold))
                    break;
     
                if (dLat > 0) pLat = wgsLat; else mLat = wgsLat;
                if (dLon > 0) pLon = wgsLon; else mLon = wgsLon;
     
                if (++i > 10000) break;
            }
            //console.log(i);
            return {'lat': wgsLat, 'lon': wgsLon};
        },
        //GCJ-02 to BD-09
        bd_encrypt : function (gcjLat, gcjLon) {
            var x = gcjLon, y = gcjLat;  
            var z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * this.x_pi);  
            var theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * this.x_pi);  
            bdLon = z * Math.cos(theta) + 0.0065;  
            bdLat = z * Math.sin(theta) + 0.006; 
            return {'lat' : bdLat,'lon' : bdLon};
        },
        //BD-09 to GCJ-02
        bd_decrypt : function (bdLat, bdLon) {
            var x = bdLon - 0.0065, y = bdLat - 0.006;  
            var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * this.x_pi);  
            var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * this.x_pi);  
            var gcjLon = z * Math.cos(theta);  
            var gcjLat = z * Math.sin(theta);
            return {'lat' : gcjLat, 'lon' : gcjLon};
        },
        //WGS-84 to Web mercator
        //mercatorLat -> y mercatorLon -> x
        mercator_encrypt : function(wgsLat, wgsLon) {
            var x = wgsLon * 20037508.34 / 180.;
            var y = Math.log(Math.tan((90. + wgsLat) * this.PI / 360.)) / (this.PI / 180.);
            y = y * 20037508.34 / 180.;
            return {'lat' : y, 'lon' : x};
            /*
            if ((Math.abs(wgsLon) > 180 || Math.abs(wgsLat) > 90))
                return null;
            var x = 6378137.0 * wgsLon * 0.017453292519943295;
            var a = wgsLat * 0.017453292519943295;
            var y = 3189068.5 * Math.log((1.0 + Math.sin(a)) / (1.0 - Math.sin(a)));
            return {'lat' : y, 'lon' : x};
            //*/
        },
        // Web mercator to WGS-84
        // mercatorLat -> y mercatorLon -> x
        mercator_decrypt : function(mercatorLat, mercatorLon) {
            var x = mercatorLon / 20037508.34 * 180.;
            var y = mercatorLat / 20037508.34 * 180.;
            y = 180 / this.PI * (2 * Math.atan(Math.exp(y * this.PI / 180.)) - this.PI / 2);
            return {'lat' : y, 'lon' : x};
            /*
            if (Math.abs(mercatorLon) < 180 && Math.abs(mercatorLat) < 90)
                return null;
            if ((Math.abs(mercatorLon) > 20037508.3427892) || (Math.abs(mercatorLat) > 20037508.3427892))
                return null;
            var a = mercatorLon / 6378137.0 * 57.295779513082323;
            var x = a - (Math.floor(((a + 180.0) / 360.0)) * 360.0);
            var y = (1.5707963267948966 - (2.0 * Math.atan(Math.exp((-1.0 * mercatorLat) / 6378137.0)))) * 57.295779513082323;
            return {'lat' : y, 'lon' : x};
            //*/
        },
        // two point's distance
        distance : function (latA, lonA, latB, lonB) {
            var earthR = 6371000.;
            var x = Math.cos(latA * this.PI / 180.) * Math.cos(latB * this.PI / 180.) * Math.cos((lonA - lonB) * this.PI / 180);
            var y = Math.sin(latA * this.PI / 180.) * Math.sin(latB * this.PI / 180.);
            var s = x + y;
            if (s > 1) s = 1;
            if (s < -1) s = -1;
            var alpha = Math.acos(s);
            var distance = alpha * earthR;
            return distance;
        },
        outOfChina : function (lat, lon) {
            if (lon < 72.004 || lon > 137.8347)
                return true;
            if (lat < 0.8293 || lat > 55.8271)
                return true;
            return false;
        },
        transformLat : function (x, y) {
            var ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
            ret += (20.0 * Math.sin(6.0 * x * this.PI) + 20.0 * Math.sin(2.0 * x * this.PI)) * 2.0 / 3.0;
            ret += (20.0 * Math.sin(y * this.PI) + 40.0 * Math.sin(y / 3.0 * this.PI)) * 2.0 / 3.0;
            ret += (160.0 * Math.sin(y / 12.0 * this.PI) + 320 * Math.sin(y * this.PI / 30.0)) * 2.0 / 3.0;
            return ret;
        },
        transformLon : function (x, y) {
            var ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
            ret += (20.0 * Math.sin(6.0 * x * this.PI) + 20.0 * Math.sin(2.0 * x * this.PI)) * 2.0 / 3.0;
            ret += (20.0 * Math.sin(x * this.PI) + 40.0 * Math.sin(x / 3.0 * this.PI)) * 2.0 / 3.0;
            ret += (150.0 * Math.sin(x / 12.0 * this.PI) + 300.0 * Math.sin(x / 30.0 * this.PI)) * 2.0 / 3.0;
            return ret;
        }
    };
    
    9、调用系统所装的软件打开一个文件的方法
    /** 
     * 打开文件 
     * @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(file);  
        //设置intent的data和Type属性。  
        intent.setDataAndType(/*uri*/Uri.fromFile(file), type);  
        //跳转  
        startActivity(intent);    
          
    }  
      
    /** 
     * 根据文件后缀名获得对应的MIME类型。 
     * @param file 
     */  
    private String getMIMEType(File file) {  
          
        String type="*/*";  
        String fName = file.getName();  
        //获取后缀名前的分隔符"."在fName中的位置。  
        int dotIndex = fName.lastIndexOf(".");  
        if(dotIndex < 0){  
            return type;  
        }  
        /* 获取文件的后缀名 */  
        String end=fName.substring(dotIndex,fName.length()).toLowerCase();  
        if(end=="")return type;  
        //在MIME和文件类型的匹配表中找到对应的MIME类型。  
        for(int i=0;i<MIME_MapTable.length;i++){ //MIME_MapTable??在这里你一定有疑问,这个MIME_MapTable是什么?  
            if(end.equals(MIME_MapTable[i][0]))  
                type = MIME_MapTable[i][1];  
        }         
        return type;  
    }  
    

    关于MIME_MapTable对应类型

    private final String[][] MIME_MapTable={
                {".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"},  
                {".docx",   "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},  
                {".xls",    "application/vnd.ms-excel"},   
                {".xlsx",   "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},  
                {".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"},  
                {".pptx",   "application/vnd.openxmlformats-officedocument.presentationml.presentation"},  
                {".prop",   "text/plain"},  
                {".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/plain"},  
                {".z",  "application/x-compress"},  
                {".zip",    "application/x-zip-compressed"},  
                {"",        "*/*"}    
            };  
    

    相关文章

      网友评论

        本文标题:Android 关于地图的一些细节

        本文链接:https://www.haomeiwen.com/subject/futhhxtx.html