問題
Android 中常用的 uri 如何使用呢 ?(此篇分析基礎(chǔ)為Android 7.1.1系統(tǒng)源碼),參看Android官方說明:https://developer.android.com/reference/android/net/Uri,代碼可在此查看:https://github.com/LineageOS/android_frameworks_base/blob/lineage-20.0/core/java/android/net/Uri.java
1 概述
??通用資源標(biāo)志符 URI (Universal Resource Identifier),URI 在 java.net.URI 中定義,顯然是 Java 提供的一個(gè)類。而 Uri 位置在 android.net.Uri 中定義(Android 源碼內(nèi)位置:frameworks/base/core/java/android/net/Uri.java),是特有針對(duì) Android 系統(tǒng)定義的類。Uri 代表要操作的數(shù)據(jù),Android 上可用的每種資源(圖像、視頻片段、網(wǎng)頁等)都可以用 Uri 來表示。
Uri 的組成部分:
- 訪問資源的命名機(jī)制(scheme)
- 存放資源的主機(jī)名(authority)
- 資源自身的名稱,由路徑表示(path)
Uri 的結(jié)構(gòu):
# 基本形式
[scheme:]scheme-specific-part[#fragment]
# 第二形式
[scheme:][//authority][path][?query][#fragment]
# 第三形式
[scheme:][//host:port][path][?query][#fragment]
特別針對(duì)scheme說明下,Uri中有這么幾種常用標(biāo)識(shí):
- content : 主要操作的是ContentProvider,它代表的是數(shù)據(jù)庫中的某個(gè)資源
- http : 某網(wǎng)站資源
- file : 本地機(jī)器上的資源
- git : git倉庫中的資源
- ftp : ftp服務(wù)器上的資源
2 Uri 的結(jié)構(gòu)
2.1 基本形式
# 基本形式
[scheme:]scheme-specific-part[#fragment]
2.2 第二形式
# 第二形式
[scheme:][//authority][path][?query][#fragment]
注意其中規(guī)則:
- path 可以有多個(gè),每個(gè)用 / 連接,比如:
scheme://authority/path1/path2/path3?query#fragment
- query 參數(shù)可以帶有對(duì)應(yīng)的值,也可以不帶,如果帶對(duì)應(yīng)的值用 = 表示,如:
//這里有一個(gè)參數(shù)id,它的值是1 scheme://authority/path1/path2/path3?id=1#fragment
- query 參數(shù)可以有多個(gè),每個(gè)用 & 連接,如:
/* 這里有3個(gè)參數(shù): 參數(shù)1:id,其值是:1 參數(shù)2:name,其值是:pedro 參數(shù)3:old,沒有對(duì)它賦值,所以它的值是null */ scheme://authority/path1/path2/path3?id = 1&name=pedro&old#fragment
??在android中,除了scheme、authority是必須要有的,其它的幾個(gè)path、query、fragment,它們每一個(gè)可以選擇性的要或不要,但順序不能變
2.3 第三形式
第二形式中 authority 又可以分為 host:port 的形式,這是劃分最細(xì)的形式:
# 第三形式
[scheme:][//host:port][path][?query][#fragment]
2.4 例子
可以通過這個(gè)例子檢測下學(xué)習(xí)效果:
http://www.pedro11.com:8080/yourpath/fileName.html?stove=10&path=32&id=4#harvic
3 Uri 的UML類圖
??我把 Android 源碼中 frameworks/base/core/java/android/net/Uri.java (也可在此查看:https://github.com/LineageOS/android_frameworks_base/blob/lineage-20.0/core/java/android/net/Uri.java)導(dǎo)入到 IDEA 工程中,通過 IDEA 的 Diagram 工具生成了它的 UML 類圖(注意其中紫色方法為抽象方法),更方便分析這個(gè)有意思的類(在Uri.java中可以看出它有很多內(nèi)部類,而且內(nèi)部類又繼承了Uri類本身)。
類圖中各個(gè)小圖標(biāo)的含義可查看這里:https://jetbrains.design/intellij/resources/icons_list/
4 從 Uri 中提取 String
- getScheme() : 獲取Uri中的scheme字符串部分,即 http
- getSchemeSpecificPart() : 獲取Uri中的scheme-specific-part:部分,即 //www.pedro11.com:8080/yourpath/fileName.html
- getFragment() : 獲取Uri中的Fragment部分,即 harvic
- getAuthority() : 獲取Uri中Authority部分,即 www.pedro11.com:8080
- getPath() : 獲取Uri中path部分,即 /yourpath/fileName.html
- getQuery() : 獲取Uri中的query部分,即 stove=10&path=32&id=4
- getHost() : 獲取Authority中的Host字符串,即 www.pedro11.com
- getPost() : 獲取Authority中的Port字符串,即 8080
- getPathSegments() : 依次提取出Path的各個(gè)部分的字符串,存入List< String>
- getQueryParameter(String key) : 通過傳進(jìn)去query中某個(gè)Key的字符串,返回它對(duì)應(yīng)的值
String mUriStr = "http://www.pedro11.com:8080/yourpath/fileName.html?stove=10&path=32&id=4#harvic";
Uri mUri = Uri.parse(mUriStr);
List<String> pathSegList = mUri.getPathSegments();
for (String pathItem:pathSegList){
Log.d("qijian","pathSegItem:"+pathItem);
}
Log.d(tag,"getQueryParameter(\"stove\"):"+mUri.getQueryParameter("stove"));
Log.d(tag,"getQueryParameter(\"id\"):"+mUri.getQueryParameter("id"));
6 Uri操作工具類
6.1 ContentUris 處理 Uri
ContentUris 有兩個(gè)作用:文章來源:http://www.zghlxwxcb.cn/news/detail-488896.html
- 為路徑加上ID
- 從Uri路徑中獲取ID
- 為Uri路徑加上ID: withAppendedId(uri, id)
//比如有這樣一個(gè)Uri Uri uri = Uri.parse("content://com.example.yy/book"); //通過ContentUris的withAppendedId()方法,為該Uri加上ID Uri resultUri = ContentUris.withAppendedId(uri, 10); //最后resultUri為: //content://com.example.yy/book/10
- 從Uri路徑中獲取ID—parseId(uri)
Uri uri = Uri.parse("content://com.example.yy/book/10") long bookId= ContentUris.parseId(uri);
6.2 UriMatcher 處理 Uri
待添加文章來源地址http://www.zghlxwxcb.cn/news/detail-488896.html
6.3 uri與file、path相互轉(zhuǎn)化
- uri 轉(zhuǎn) file
file = new File(new URI(uri.toString()));
- file 轉(zhuǎn) uri
URI uri = file.toURI();
- uri 轉(zhuǎn) path
private String getPath(Context context, Uri uri) { String path = null; Cursor cursor = context.getContentResolver().query(uri, null, null, null, null); if (cursor == null) { return null; } if (cursor.moveToFirst()) { try { path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)); } catch (Exception e) { e.printStackTrace(); } } cursor.close(); return path; }
- path 轉(zhuǎn) uri
Uri uri = Uri.parse(path);
- file 轉(zhuǎn) path
String path = file.getPath();
- path 轉(zhuǎn) file
File file = new File(path);
8 常用Uri
//顯示網(wǎng)頁:
Uri uri = Uri.parse("http://www.google.com");
Intent it = new Intent(Intent.ACTION_VIEW,uri);
startActivity(it);
//顯示地圖:
Uri uri = Uri.parse("geo:38.899533,-77.036476");
Intent it = new Intent(Intent.Action_VIEW,uri);
startActivity(it);
//路徑規(guī)劃:
Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en");
Intent it = new Intent(Intent.ACTION_VIEW,URI);
startActivity(it);
//調(diào)用撥號(hào)程序,要使用這個(gè)必須在配置文件中加入<uses-permission id="Android.permission.CALL_PHONE" />
Uri uri = Uri.parse("tel:xxxxxx");
Intent it = new Intent(Intent.ACTION_DIAL, uri);
startActivity(it);
Uri uri = Uri.parse("tel.xxxxxx");
Intent it =new Intent(Intent.ACTION_CALL,uri);
//調(diào)用發(fā)送短信的程序
Intent it = new Intent(Intent.ACTION_VIEW);
it.putExtra("sms_body", "The SMS text");
it.setType("vnd.android-dir/mms-sms");
startActivity(it);
//發(fā)送短信
Uri uri = Uri.parse("smsto:0800000123");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
it.putExtra("sms_body", "The SMS text");
startActivity(it);
//發(fā)送彩信
Uri uri = Uri.parse("content://media/external/images/media/23");
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra("sms_body", "some text");
it.putExtra(Intent.EXTRA_STREAM, uri);
it.setType("image/png");
startActivity(it);
//發(fā)送Email
Uri uri = Uri.parse("mailto:xxx@abc.com");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
startActivity(it);
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_EMAIL, "me@abc.com");
it.putExtra(Intent.EXTRA_TEXT, "The email body text");
it.setType("text/plain");
startActivity(Intent.createChooser(it, "Choose Email Client"));
Intent it=new Intent(Intent.ACTION_SEND);
String[] tos={"me@abc.com"};
String[] ccs={"you@abc.com"};
it.putExtra(Intent.EXTRA_EMAIL, tos);
it.putExtra(Intent.EXTRA_CC, ccs);
it.putExtra(Intent.EXTRA_TEXT, "The email body text");
it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
it.setType("message/rfc822");
startActivity(Intent.createChooser(it, "Choose Email Client"));
//添加附件
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
it.putExtra(Intent.EXTRA_STREAM, "[url=]file:///sdcard/mysong.mp3[/url]");
sendIntent.setType("audio/mp3");
startActivity(Intent.createChooser(it, "Choose Email Client"));
//播放多媒體
Intent it = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("[url=]file:///sdcard/song.mp3[/url]");
it.setDataAndType(uri, "audio/mp3");
startActivity(it);
Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
//Uninstall APP
Uri uri = Uri.fromParts("package", strPackageName, null);
Intent it = new Intent(Intent.ACTION_DELETE, uri);
startActivity(it);
//調(diào)用相冊(cè)
public static final String MIME_TYPE_IMAGE_JPEG = "image/*";
public static final int ACTIVITY_GET_IMAGE = 0;
Intent getImage = new Intent(Intent.ACTION_GET_CONTENT);
getImage.addCategory(Intent.CATEGORY_OPENABLE);
getImage.setType(MIME_TYPE_IMAGE_JPEG);
startActivityForResult(getImage, ACTIVITY_GET_IMAGE);
//調(diào)用系統(tǒng)相機(jī)應(yīng)用程序,并存儲(chǔ)拍下來的照片
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
time = Calendar.getInstance().getTimeInMillis();
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/tucue", time + ".jpg")));
startActivityForResult(intent, ACTIVITY_GET_CAMERA_IMAGE);
//play audio
Uri playUri = Uri.parse("[url=]file:///sdcard/download/everything.mp3[/url]");
returnIt = new Intent(Intent.ACTION_VIEW, playUri);
//發(fā)送附件
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
it.putExtra(Intent.EXTRA_STREAM, "[url=]file:///sdcard/eoe.mp3[/url]");
sendIntent.setType("audio/mp3");
startActivity(Intent.createChooser(it, "Choose Email Client"));
//搜索應(yīng)用
Uri uri = Uri.parse("market://search?q=pname:pkg_name");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
//進(jìn)入聯(lián)系人頁面
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(People.CONTENT_URI);
startActivity(intent);
//查看指定聯(lián)系人
Uri personUri = ContentUris.withAppendedId(People.CONTENT_URI, info.id);//info.id聯(lián)系人ID
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(personUri);
startActivity(intent);
到了這里,關(guān)于Android系統(tǒng)的問題分析筆記(9) - Android 中的 Uri 如何使用呢 ?的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!