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:admin@3gstdy.com
  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:nobody@google.com".
  • 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




转载于:https://www.cnblogs.com/baiqiantao/p/7256922.html

package com.music.zhangdaosen; import android.annotation.SuppressLint; import android.content.ContentResolver; import android.content.Context; import android.database.Cursor; import android.net.Uri; import android.provider.MediaStore; import android.util.Log; import android.widget.ArrayAdapter; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; public class SongHelper { public static List<Song>musicList = new ArrayList<>(); // static List<Song>musicList; public static List<Song> getLocalMusic(Context context){ Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; ContentResolver resolver = context.getContentResolver(); Cursor cursor = resolver.query(uri,null,null,null,null); if(cursor !=null){ Song song; while (cursor.moveToNext()){ song =new Song(); int index = cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME); song.name = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME)); song.singer = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST)); song.path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)); song.duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)); Log.d("SongHelper--keyar","get"+song.name+" "+song.duration); musicList.add(song); SimpleDateFormat sdf = new SimpleDateFormat("mm:ss"); String time = sdf.format(new Date(song.duration)); } Log.d("SongHelper--keyar","get musicList"+ musicList.size()); } cursor.close(); return musicList; } public static String changeTime(int duration){ int min = duration/1000/60; int sec = duration/1000%60; if(sec<10){ return min+":0"+sec; } return min+":"+sec; } }
最新发布
06-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值