1. 程式人生 > >android.net.Uri 簡介 API

android.net.Uri 簡介 API

put read other getpath .net gets 構建 follow ast

android.net.Uri 簡介

  1. public abstract class android.net.Uri extends Object implements Parcelable, Comparable<Uri>
Immutable URI reference. A URI reference includes a URI and a fragment, the component of the URI following a ‘#‘. Builds and parses URI references which conform to RFC 2396.不可變的URI引用。 一個URI引用包含一個URI和一個
fragment(即URI中在‘#‘後面的組件)。 構建和解析符合RFC 2396的URI引用。
In the interest of performance, this class performs little to no validation. Behavior is undefined for invalid input. This class is very forgiving--in the face of invalid input, it will return garbage rather than throw an exception unless otherwise specified.
為了更好的表現,這個類執行很少甚至沒有驗證。 對於無效輸入,其行為是未定義的。 這個類對無效的輸入包容性很強,除非另有說明,否則它將返回垃圾(垃圾; 臟東西; 丟棄的食物; 無用的數據)而不是拋出異常。
URI:通用資源標誌符(Universal Resource Identifier)。Uri 代表要操作的數據,Android上可用的每種資源 - 圖像、視頻片段等都可以用Uri來表示。

Uri 的組成及示例

就Android平臺而言,Uri主要分四個部分:scheme, authority and path, query。其中authority又分為host和port。基本格式如下:
  1. scheme://host:port/path?query
官方文檔中對Uri的格式描述如下
  1. <scheme>://<authority><absolute path>?<query>#<fragment>

舉個實際的例子:content://com.example.project:200/folder/subfolder/etc?name=bqt&age=28\----------/ \-------------------------/ \----/ \------------------------/ \------------------------/scheme host port path query \-----------------------------/ authority
各個參數的含義如下:
  • 1)scheme:訪問資源的命名機制,通過這個可以獲悉Uri的具體資源存在形式,如http、content、file、market等
  • 2)authority:存放資源的主機名。authority 應該是scheme:// 之後到第一次出現 ‘/’ 或‘?’ 或‘#’之間的string
  • 3)path:authority之後第一個 ‘/’ 開始到 ‘?’ 或 ‘#’ 之前的string(包含‘/‘)
  • 4)query:‘?‘ 號之後 ‘#‘ 號之前的string(類似get請求時的提交的參數)

經常看到的Uri形式有:
  1. 打開一個網頁 http://blog.3gstdy.com/
  2. 發送短信 smsto:10086
  3. 發送彩信(相當於發送帶附件的短信) content://media/external/images/media/23
  4. 打開地圖並定位到一個點 geo:52.76,-79.0342
  5. 撥打電話 tel:10086
  6. 打開文件 file:///sdcard/download/everything.mp3
  7. 打開發郵件界面 mailto:[email protected]
  8. 尋找某個應用 market://search?q=pname:pkg_name
  9. 顯示地圖(經緯度) geo:39.9,116.3
  10. 路徑規劃 http://maps.google .com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en

現在大家應該知道data flag中那些屬性的含義了吧,看下data flag
  1. <data android:host="string"
  2. android:mimeType="string"
  3. android:path="string"
  4. android:pathPattern="string"
  5. android:pathPrefix="string"
  6. android:port="string"
  7. android:scheme="string" />

get**和getEncoded**等方法的區別

關於如getPath和getEncodedPath等方法的區別:如果裏面有出現非A~Z、a~z、0~9、‘_’、‘-’、‘嘆號’、‘點號’、‘~’、‘單引號’、‘(’、‘)’、‘星號’的字符,都需要encode一把,有時候會看到%連接的一串,那都是encode。
系統實現方式為:
  1. for (int i = 0; i < bytesLength; i++) {
  2. encoded.append(‘%‘);
  3. encoded.append(HEX_DIGITS[(bytes[i] & 0xf0) >> 4]);
  4. encoded.append(HEX_DIGITS[bytes[i] & 0xf]);
  5. }
演示示例
  1. Uri uri = new Uri.Builder().path("包青天").build();
  2. Log.i("bqt", uri.getPath() + " " + uri.getEncodedPath());//包青天 %E5%8C%85%E9%9D%92%E5%A4%A9
  3. Uri uri1 = new Uri.Builder().encodedPath("%E5%8C%85%E9%9D%92%E5%A4%A9").build();
  4. Log.i("bqt", uri1.getPath() + " " + uri1.getEncodedPath());//包青天 %E5%8C%85%E9%9D%92%E5%A4%A9
  5. Uri uri2 = new Uri.Builder().path("bqt").build();
  6. Log.i("bqt", uri2.getPath() + " " + uri2.getEncodedPath());//bqt bqt
  7. Uri uri3 = new Uri.Builder().encodedPath("包青天").build();
  8. Log.i("bqt", uri3.getPath() + " " + uri3.getEncodedPath());//包青天 包青天

android.net.Uri【API】

靜態方法
  • static String decode(String s) Decodes ‘%‘-escaped octets in the given string using the UTF-8 scheme.
  • static String encode(String s, String allow) Encodes characters in the given string as ‘%‘-escaped octets using the UTF-8 scheme.
  • static String encode(String s) Encodes characters in the given string as ‘%‘-escaped octets using the UTF-8 scheme.
  • static Uri fromFile(File file) Creates a Uri from a file.
  • static Uri fromParts(String scheme, String ssp, String fragment) Creates an opaque Uri from the given components.
  • static Uri parse(String uriString) Creates a Uri which parses the given encoded URI string.
  • static Uri withAppendedPath(Uri baseUri, String pathSegment) Creates a new Uri by appending an already-encoded path segment to a base Uri.

普通方法
  • String getQueryParameter(String key) Searches the query string for the first value with the given key.
  • Set<String> getQueryParameterNames() Returns a set of the unique names of all query parameters.
  • List<String> getQueryParameters(String key) Searches the query string for parameter values with the given key.
  • boolean getBooleanQueryParameter(String key, boolean defaultValue) Searches the query string for the first value with the given key and interprets it as a boolean value.
  • boolean isAbsolute() Returns true if this URI is absolute, i.e. if it contains an explicit明確的 scheme.
  • boolean isOpaque() Returns true if this URI is opaque不透明的、模糊的 like "mailto:[email protected]".
  • Uri normalizeScheme() Return an equivalent等價的 URI with a lowercase小寫的 scheme component組成. normalize:使正常化、使標準化

抽象方法get方法
  • abstract String getEncodedAuthority() Gets the encoded/decoded authority part of this URI.
  • abstract String getEncodedFragment() Gets the encoded/decoded fragment part of this URI, everything after the ‘#‘.
  • abstract String getEncodedPath() Gets the encoded/decoded path.
  • abstract String getEncodedQuery() Gets the encoded/decoded query component from this URI.
  • abstract String getHost() Gets the encoded host from the authority for this URI.
  • abstract int getPort() Gets the port from the authority for this URI.
  • abstract String getScheme() Gets the scheme of this URI. Example: "http"
  • abstract String getEncodedUserInfo() Gets the encoded user information from the authority.
path segments
  • abstract String getLastPathSegment() Gets the decoded last segment in the path.
  • abstract List<String> getPathSegments() Gets the decoded path segments.
scheme-specific part
  • abstract String getSchemeSpecificPart() Gets the scheme-specific part of this URI, i.e. everything between the scheme separator ‘:‘ and the fragment separator ‘#‘.
  • abstract String getEncodedSchemeSpecificPart() Gets the scheme-specific part of this URI, i.e. everything between the scheme separator ‘:‘ and the fragment separator ‘#‘.
其他方法
  • abstract Uri.Builder buildUpon() Constructs a new builder, copying the attributes from this Uri.
  • abstract boolean isHierarchical() Returns true if this URI is hierarchical like "http://google.com".
  • abstract boolean isRelative() Returns true if this URI is relative, i.e. if it doesn‘t contain an explicit明確的 scheme.
  • abstract String toString() Returns the encoded string representation of this URI.

Uri.Builder【文檔及API】

Helper class for building or manipulating操控、操縱 URI references. Not safe for concurrent並發 use.
An absolute hierarchical分層、層次 URI reference follows遵循 the pattern模式:
  1. <scheme>://<authority><absolute path>?<query>#<fragment>

Relative URI references (which are always hierarchical) follow one of two patterns:
  1. <relative or absolute path>?<query>#<fragment>
or
  1. //<authority><absolute path>?<query>#<fragment>

An opaque不透明的 URI follows this pattern:
  1. <scheme>:<opaque part>#<fragment>

Use buildUpon() to obtain獲取 a builder representing代表 an existing URI.
build
  • Uri build() Constructs a Uri with the current attributes.
authority
  • Uri.Builder authority(String authority) Encodes and sets the authority.
  • Uri.Builder encodedAuthority(String authority) Sets the previously encoded authority.
path
  • Uri.Builder path(String path) Sets the path.
  • Uri.Builder encodedPath(String path) Sets the previously encoded path.
  • Uri.Builder appendPath(String newSegment) Encodes the given segment and appends it to the path.
  • Uri.Builder appendEncodedPath(String newSegment) Appends the given segment片段、分割 to the path.
query
  • Uri.Builder query(String query) Encodes and sets the query.
  • Uri.Builder encodedQuery(String query) Sets the previously encoded query.
  • Uri.Builder appendQueryParameter(String key, String value) Encodes the key and value and then appends the parameter to the query string.
  • Uri.Builder clearQuery() Clears the the previously set query.
scheme
  • Uri.Builder scheme(String scheme) Sets the scheme.
fragment
  • Uri.Builder fragment(String fragment) Encodes and sets the fragment.
  • Uri.Builder encodedFragment(String fragment) Sets the previously encoded fragment.
opaquePart
  • Uri.Builder opaquePart(String opaquePart) Encodes and sets the given opaque scheme-specific-part.
  • Uri.Builder encodedOpaquePart(String opaquePart) Sets the previously encoded opaque scheme-specific-part.
2017-7-29

null

android.net.Uri 簡介 API