android.net.Uri 简介 API

【android.net.Uri 简介 API】一万年来谁著史,三千里外欲封侯。这篇文章主要讲述android.net.Uri 简介 API相关的知识,希望能为你提供帮助。
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()); //bqtbqt
  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





































































    推荐阅读