有時(shí)候我們需要調(diào)用地圖顯示一下位置坞琴,這時(shí)候可能還需要導(dǎo)航
1、判斷手機(jī)上有沒(méi)有安裝各種地圖
/**
* @描述 檢查手機(jī)上是否安裝了地圖
* @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;
}
另外有一種辦法是通過(guò)循環(huán)包名判斷系統(tǒng)是否有包
/**
* 檢查手機(jī)上是否安裝了指定的軟件
* @param context
* @param packageName:應(yīng)用包名
* @return
*/
public static boolean isAvilible(Context context, String packageName){
//獲取packagemanager
final PackageManager packageManager = context.getPackageManager();
//獲取所有已安裝程序的包信息
List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
//用于存儲(chǔ)所有已安裝程序的包名
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中是否有目標(biāo)程序的包名伴挚,有TRUE阎抒,沒(méi)有FALSE
return packageNames.contains(packageName);
}
2醒叁、各種應(yīng)用打開(kāi)的列子:
如百度:
if(isAvilible(context,"com.baidu.BaiduMap")){//傳入指定應(yīng)用包名
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&" + //起點(diǎn) 此處不傳值默認(rèn)選擇當(dāng)前位置
"destination=latlng:"+location[0]+","+location[1]+"|name:我的目的地"+ //終點(diǎn)
"&mode=driving&" + //導(dǎo)航路線(xiàn)方式
"region=北京" + //
"&src=慧醫(yī)#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end");
context.startActivity(intent); //啟動(dòng)調(diào)用
} catch (URISyntaxException e) {
Log.e("intent", e.getMessage());
}
}else{//未安裝
//market為路徑司浪,id為包名
//顯示手機(jī)上所有的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=慧醫(yī)&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、網(wǎng)頁(yè)打開(kāi)地圖
如百度
private void openBrowserBaidu() {
Uri webpage = Uri.parse("http://api.map.baidu.com/marker?location=39.915168,116.403875&title=目的地&content=天安門(mén)&output=html");
Intent webIntent = new Intent(Intent.ACTION_VIEW,webpage);
startActivity(webIntent);
}
4把沼、各個(gè)地圖的網(wǎng)站
百度地圖: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啊易、調(diào)用系統(tǒng)geo-uri方式調(diào)用外部程序方法來(lái)打開(kāi)地圖(不建議使用)
//geo:latitude,longitude
//geo:latitude,longitude?z=zoom,z表示zoom級(jí)別饮睬,值為數(shù)字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=西直門(mén)");
Intent mIntent = new Intent(Intent.ACTION_VIEW,mUri);
startActivity(mIntent);
這段代碼將會(huì)彈出一個(gè)對(duì)話(huà)框租谈,顯示所有在initer-filter中注冊(cè)了geo-uri類(lèi)型的程序,讓用戶(hù)進(jìn)行選擇捆愁,如果我們的程序也需要支持處理geo-uri垦垂,可以通過(guò)在AndroidMainfest文件中添加如下代碼來(lái)實(shí)現(xiàn):
<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進(jìn)行打包的時(shí)候會(huì)提示initer-filter要放進(jìn)<activity>標(biāo)簽內(nèi),無(wú)法打包成功牙瓢。
但是如果你把這個(gè)方法放進(jìn)你的地圖里面,它會(huì)默認(rèn)把你的那個(gè)模塊也列進(jìn)去间校,所以有bug矾克。而且百度地圖、高德地圖的坐標(biāo)位置都是不對(duì)應(yīng)憔足,需要轉(zhuǎn)換的胁附,所以此辦法如果是傳經(jīng)緯度導(dǎo)航可能會(huì)偏差。
6滓彰、其他各種地圖坐標(biāo)轉(zhuǎn)工具類(lèi)(已測(cè)過(guò)控妻,跟IOS的轉(zhuǎn)換結(jié)果一模一樣)
/**
* @創(chuàng)建 HaiJia
* @時(shí)間 2017/8/29 16:42
* @描述
* 各地圖API坐標(biāo)系統(tǒng)比較與轉(zhuǎn)換;
* WGS84坐標(biāo)系:即地球坐標(biāo)系,國(guó)際上通用的坐標(biāo)系揭绑。設(shè)備一般包含GPS芯片或者北斗芯片獲取的經(jīng)緯度為WGS84地理坐標(biāo)系,
* 谷歌地圖采用的是WGS84地理坐標(biāo)系(中國(guó)范圍除外);
* GCJ02坐標(biāo)系:即火星坐標(biāo)系弓候,是由中國(guó)國(guó)家測(cè)繪局制訂的地理信息系統(tǒng)的坐標(biāo)系統(tǒng)郎哭。由WGS84坐標(biāo)系經(jīng)加密后的坐標(biāo)系。
* 谷歌中國(guó)地圖和搜搜中國(guó)地圖采用的是GCJ02地理坐標(biāo)系; BD09坐標(biāo)系:即百度坐標(biāo)系菇存,GCJ02坐標(biāo)系經(jīng)加密后的坐標(biāo)系;
* 搜狗坐標(biāo)系夸研、圖吧坐標(biāo)系等,估計(jì)也是在GCJ02基礎(chǔ)上加密而成的依鸥。
*/
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 火星坐標(biāo)系 (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);
}
/**
* * 火星坐標(biāo)系 (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);
}
/**
* 火星坐標(biāo)系 (GCJ-02) 與百度坐標(biāo)系 (BD-09) 的轉(zhuǎn)換算法 將 GCJ-02 坐標(biāo)轉(zhuǎn)換成 BD-09 坐標(biāo)
*
* @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);
}
/**
* * 火星坐標(biāo)系 (GCJ-02) 與百度坐標(biāo)系 (BD-09) 的轉(zhuǎn)換算法 * * 將 BD-09 坐標(biāo)轉(zhuǎn)換成GCJ-02 坐標(biāo) * * @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) {
// 北斗芯片獲取的經(jīng)緯度為WGS84地理坐標(biāo) 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 對(duì)象如下
/**
* @創(chuàng)建 HaiJia
* @時(shí)間 2017/8/29 16:41
* @描述 Gps 對(duì)象
*/
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亥至、地理坐標(biāo)的一些知識(shí)普及
/**
*
* API 坐標(biāo)系
* CLLocationManager 地球坐標(biāo) (WGS84)
* Google 衛(wèi)星地圖 地球坐標(biāo) (WGS84)
* 百度地圖API 百度坐標(biāo)(BD-09)
* 騰訊搜搜地圖API 火星坐標(biāo)(GCJ-02)
* iOS地圖 火星坐標(biāo)(GCJ-02)
* 阿里云地圖API 火星坐標(biāo)(GCJ-02)
* 高德MapABC地圖API 火星坐標(biāo)(GCJ-02)
* 靈圖51ditu地圖API 火星坐標(biāo)(GCJ-02)
* @brief 世界標(biāo)準(zhǔn)地理坐標(biāo)(WGS-84) 轉(zhuǎn)換成 中國(guó)國(guó)測(cè)局地理坐標(biāo)(GCJ-02)<火星坐標(biāo)>
*
* ####只在中國(guó)大陸的范圍的坐標(biāo)有效,以外直接返回世界標(biāo)準(zhǔn)坐標(biāo)
*
* location 世界標(biāo)準(zhǔn)地理坐標(biāo)(WGS-84)
**/
8贱迟、另外附上前端的地球坐標(biāo)轉(zhuǎn)火星坐標(biāo)的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: 衛(wèi)星橢球坐標(biāo)投影到平面地圖坐標(biāo)系的投影因子姐扮。
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衣吠、調(diào)用系統(tǒng)所裝的軟件打開(kāi)一個(gè)文件的方法
/**
* 打開(kāi)文件
* @param file
*/
private void openFile(File file){
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//設(shè)置intent的Action屬性
intent.setAction(Intent.ACTION_VIEW);
//獲取文件file的MIME類(lèi)型
String type = getMIMEType(file);
//設(shè)置intent的data和Type屬性茶敏。
intent.setDataAndType(/*uri*/Uri.fromFile(file), type);
//跳轉(zhuǎn)
startActivity(intent);
}
/**
* 根據(jù)文件后綴名獲得對(duì)應(yīng)的MIME類(lèi)型。
* @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和文件類(lèi)型的匹配表中找到對(duì)應(yīng)的MIME類(lèi)型睡榆。
for(int i=0;i<MIME_MapTable.length;i++){ //MIME_MapTable??在這里你一定有疑問(wèn),這個(gè)MIME_MapTable是什么袍榆?
if(end.equals(MIME_MapTable[i][0]))
type = MIME_MapTable[i][1];
}
return type;
}
關(guān)于MIME_MapTable對(duì)應(yīng)類(lèi)型
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"},
{"", "*/*"}
};