这篇文章最核心的就是去学习如何学习Android,如何去使用Android文档。
我们一般在刚开始接触开发的时候,如果遇到无法解决的问题,常常会百度,或者google去寻找答案,比如有个需求是获取系统中的图片,你可能会直接去搜索这个功能相关的码,如果需求再后来发生了变更,可能还回去网上找代码,万一你遇到的问题在网上找不到呢?
我们还是拿获取系统图片这个需求来举例说明,我们不去网上根据关键词搜索,如果只是查API,你会怎么解决这个问题呢?
有以下解决方法:
1.自己写一个实现。
2.使用系统提供的功能方法。
如果是选择第一种,可能你还不知道系统为我们提供了这样的功能,实现起来的可能就会耗时耗力,最后的结果也不是很理想,这是下策。
如果选择第二种,可能你已经对Android系统有个大概的了解了,知道如何是使用Android已经为我们提供好的功能。
好,接下来,我们就依据第二种解决办法来实现我们的功能,并附带思想上的解决方法:
那既然是要获取很多资源,那必然是需要使用ContentProvider这个组件了,ContentProvider的功能是给外部提供数据访问的接口,这里我们是要获取,正好想法,我们需要使用的是ContentResolver来解析外部数据,那怎么获取这个对象的引用呢?在Android中如果要使用系统提供的资源,一般需要使用Context,我们这里我们就可以通过Context.getContentResolver()来获取。
OK,获取到这个对象之后怎么使用呢?ContentResolver提供了增删改查等基本操作,我们这里还是获取数据,所以是查询,需要用到查询方法query。
Cursor query (Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
Query the given URI, returning a Cursor
over the result set.
For best performance, the caller should follow these guidelines:
- Provide an explicit projection, to prevent reading data from storage that aren't going to be used.
- Use question mark parameter markers such as 'phone=?' instead of explicit values in the
selection
parameter, so that queries that differ only by those values will be recognized as the same for caching purposes.
Parameters
uri | The URI, using the content:// scheme, for the content to retrieve. |
---|---|
projection | A list of which columns to return. Passing null will return all columns, which is inefficient. |
selection | A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given URI. |
selectionArgs | You may include ?s in selection, which will be replaced by the values from selectionArgs, in the order that they appear in the selection. The values will be bound as Strings. |
sortOrder | How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort order, which may be unordered. |
Returns
- A Cursor object, which is positioned before the first entry, or null
我们先打开Android官方的开发文档,找到ContentProvider Guide这一页,这页对ContentProvider的使用有个简要的说明,其中使用了手机中的“联系人”做了举例说明:
我们可以点开ContactsContract.Contacts这个链接,可以看到这个页面对联系人有个详细说明,在页面中有一部分列举了各种Uri:
Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
public static final Uri | CONTENT_FILTER_URI | The content:// style URI used for "type-to-filter" functionality on the CONTENT_URI URI. | |||||||||
public static final Uri | CONTENT_FREQUENT_URI | The content:// style URI for showing a list of frequently contacted people. | |||||||||
public static final Uri | CONTENT_GROUP_URI | ||||||||||
public static final Uri | CONTENT_LOOKUP_URI | A content:// style URI for this table that should be used to create shortcuts or otherwise create long-term links to contacts. | |||||||||
public static final Uri | CONTENT_MULTI_VCARD_URI | Base Uri for referencing multiple Contacts entry, created by appending LOOKUP_KEY using withAppendedPath(Uri, String) . | |||||||||
public static final Uri | CONTENT_STREQUENT_FILTER_URI | The content:// style URI used for "type-to-filter" functionality on the CONTENT_STREQUENT_URI URI. | |||||||||
public static final Uri | CONTENT_STREQUENT_URI | The content:// style URI for this table joined with useful data from ContactsContract.Data , filtered to include only starred contacts and the most frequently contacted contacts. | |||||||||
public static final Uri | CONTENT_URI | The content:// style URI for this table | |||||||||
public static final Uri | CONTENT_VCARD_URI | Base Uri for referencing a single Contacts entry, created by appending LOOKUP_KEY using withAppendedPath(Uri, String) . |
刚才我们点开的ContactsContract.Contacts这个链接,可以看到它的包名是:
android.provider.ContactsContract.Contacts |
也就是说,在这个包下面的类都是android为我们提供好的、可以直接使用的内容提供者,我们通过左侧的导航打开这个android.provider这个包:
发现有好多好多类:
AlarmClock | The AlarmClock provider contains an Intent action and extras that can be used to start an Activity to set a new alarm or timer in an alarm clock application. |
Browser | |
CalendarContract | The contract between the calendar provider and applications. |
CalendarContract.Attendees | Fields and helpers for interacting with Attendees. |
CalendarContract.CalendarAlerts | Fields and helpers for accessing calendar alerts information. |
CalendarContract.CalendarCache | CalendarCache stores some settings for calendar including the current time zone for the instances. |
CalendarContract.CalendarEntity | Class that represents a Calendar Entity. |
CalendarContract.Calendars | Constants and helpers for the Calendars table, which contains details for individual calendars. |
CalendarContract.Colors | Fields for accessing colors available for a given account. |
CalendarContract.EventDays | Fields and helpers for querying for a list of days that contain events. |
CalendarContract.Events | Constants and helpers for the Events table, which contains details for individual events. |
CalendarContract.EventsEntity | Class that represents an Event Entity. |
CalendarContract.ExtendedProperties | Fields for accessing the Extended Properties. |
CalendarContract.Instances | Fields and helpers for interacting with Instances. |
CalendarContract.Reminders | Fields and helpers for accessing reminders for an event. |
CalendarContract.SyncState | A table provided for sync adapters to use for storing private sync state data. |
CallLog | The CallLog provider contains information about placed and received calls. |
CallLog.Calls | Contains the recent calls. |
MediaStore | The Media provider contains meta data for all available media on both internal and external storage devices. |
MediaStore.Audio | Container for all audio content. |
MediaStore.Audio.Albums | Contains artists for audio files |
MediaStore.Audio.Artists | Contains artists for audio files |
MediaStore.Audio.Artists.Albums | Sub-directory of each artist containing all albums on which a song by the artist appears. |
MediaStore.Audio.Genres | Contains all genres for audio files |
MediaStore.Audio.Genres.Members | Sub-directory of each genre containing all members. |
MediaStore.Audio.Media | |
MediaStore.Audio.Playlists | Contains playlists for audio files |
MediaStore.Audio.Playlists.Members | Sub-directory of each playlist containing all members. |
MediaStore.Audio.Radio | |
MediaStore.Files | Media provider table containing an index of all files in the media storage, including non-media files. |
MediaStore.Images | Contains meta data for all available images. |
MediaStore.Images.Media | |
MediaStore.Images.Thumbnails | This class allows developers to query and get two kinds of thumbnails: MINI_KIND: 512 x 384 thumbnail MICRO_KIND: 96 x 96 thumbnail |
MediaStore.Video | |
MediaStore.Video.Media | |
MediaStore.Video.Thumbnails | This class allows developers to query and get two kinds of thumbnails: MINI_KIND: 512 x 384 thumbnail MICRO_KIND: 96 x 96 thumbnail |
Nested Classes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
interface | MediaStore.Images.ImageColumns | ||||||||||
class | MediaStore.Images.Media | ||||||||||
class | MediaStore.Images.Thumbnails | This class allows developers to query and get two kinds of thumbnails: MINI_KIND: 512 x 384 thumbnail MICRO_KIND: 96 x 96 thumbnail |
Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
public static final Uri | EXTERNAL_CONTENT_URI | The content:// style URI for the "primary" external storage volume. | |||||||||
public static final Uri | INTERNAL_CONTENT_URI | The content:// style URI for the internal storage. |
ContentResolver contentResolver = mContext.getContentResolver();Cursor query = contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
最后根据获取到的Cursor对象来查询我们想要的信息:
ContentResolver contentResolver = mContext.getContentResolver();Cursor query = contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);int columnCount = query.getColumnCount();while (query.moveToNext()) {StringBuilder stringBuilder = new StringBuilder();for (int i = 0; i < columnCount; i++) {int type = query.getType(i);//获取数据类型String columnName = query.getColumnName(i);//获取列名stringBuilder.append(columnName + " : ");//获得查询结果if (type == Cursor.FIELD_TYPE_STRING) {String string = query.getString(i);stringBuilder.append(string + " , ");} else if (type == Cursor.FIELD_TYPE_INTEGER) {int anInt = query.getInt(i);stringBuilder.append(anInt + " , ");}}Log.i("EXTERNAL_CONTENT_URI", stringBuilder.toString());}
打印日志(略去部分信息):
I/EXTERNAL_CONTENT_URI: _id : 24181 , _data : /storage/sdcard0/Pictures/Screenshots/Screenshot_2015-02-03-15-24-42.png I/EXTERNAL_CONTENT_URI: _id : 24182 , _data : /storage/sdcard0/Pictures/Screenshots/Screenshot_2015-02-03-15-24-45.png I/EXTERNAL_CONTENT_URI: _id : 24183 , _data : /storage/sdcard0/Pictures/Screenshots/Screenshot_2015-02-09-15-30-18.png I/EXTERNAL_CONTENT_URI: _id : 24184 , _data : /storage/sdcard0/Pictures/Screenshots/Screenshot_2015-03-01-11-54-13.png I/EXTERNAL_CONTENT_URI: _id : 27983 , _data : /storage/sdcard0/Tencent/Tencentnews/download/89080ef7a854b5fbfa7aaab86deb38bf.jpg I/EXTERNAL_CONTENT_URI: _id : 27984 , _data : /storage/sdcard0/Tencent/Tencentnews/download/5b1a9db2a612c56903667fd5ced07690.jpg I/EXTERNAL_CONTENT_URI: _id : 27985 , _data : /storage/sdcard0/Tencent/Tencentnews/download/94e91ec0761c9d0e775bcbec65238fa7.jpg I/EXTERNAL_CONTENT_URI: _id : 27986 , _data : /storage/sdcard0/xtuone/friday/note/default_note.png I/EXTERNAL_CONTENT_URI: _id : 29187 , _data : /storage/sdcard0/BaiduMap/cache/assets/subway/images/citylist.png I/EXTERNAL_CONTENT_URI: _id : 29188 , _data : /storage/sdcard0/BaiduMap/cache/assets/subway/images/icon_beijing_ap2.png I/EXTERNAL_CONTENT_URI: _id : 29189 , _data : /storage/sdcard0/BaiduMap/cache/assets/subway/images/icon_beijing_ap3.png I/EXTERNAL_CONTENT_URI: _id : 29190 , _data : /storage/sdcard0/BaiduMap/cache/assets/subway/images/icon_shanghai_ap.png I/EXTERNAL_CONTENT_URI: _id : 29191 , _data : /storage/sdcard0/BaiduMap/cache/assets/subway/images/loading.gif I/EXTERNAL_CONTENT_URI: _id : 29192 , _data : /storage/sdcard0/BaiduMap/cache/assets/subway/images/transfer.png I/EXTERNAL_CONTENT_URI: _id : 29193 , _data : /storage/sdcard0/BaiduMap/cache/assets/subway/images/transparent.gif I/EXTERNAL_CONTENT_URI: _id : 29532 , _data : /storage/sdcard0/Tencent/Tencentnews/download/0d19d995e981b74534a62625f4aee3ff.jpg I/EXTERNAL_CONTENT_URI: _id : 29533 , _data : /storage/sdcard0/Tencent/Tencentnews/download/262c3ebb8ec005fda0f020de8aaf0c27.jpg I/EXTERNAL_CONTENT_URI: _id : 29534 , _data : /storage/sdcard0/Tencent/Tencentnews/download/e6f74c620519a1c538832f1d78b02e0e.jpg I/EXTERNAL_CONTENT_URI: _id : 35369 , _data : /storage/sdcard0/Pictures/Screenshots/Screenshot_2015-07-10-13-51-27.png I/EXTERNAL_CONTENT_URI: _id : 36466 , _data : /storage/sdcard0/Backucup/com.UCMobile/homepage/appcenter/10578.bmp I/EXTERNAL_CONTENT_URI: _id : 36467 , _data : /storage/sdcard0/Backucup/com.UCMobile/homepage/appcenter/10598.bmp I/EXTERNAL_CONTENT_URI: _id : 36468 , _data : /storage/sdcard0/Backucup/com.UCMobile/homepage/appcenter/10614.bmp I/EXTERNAL_CONTENT_URI: _id : 36469 , _data : /storage/sdcard0/Backucup/com.UCMobile/homepage/appcenter/10628.bmp I/EXTERNAL_CONTENT_URI: _id : 36470 , _data : /storage/sdcard0/Backucup/com.UCMobile/homepage/appcenter/10681.bmp I/EXTERNAL_CONTENT_URI: _id : 36471 , _data : /storage/sdcard0/Backucup/com.UCMobile/homepage/appcenter/10461.bmp I/EXTERNAL_CONTENT_URI: _id : 36472 , _data : /storage/sdcard0/Backucup/com.UCMobile/homepage/appcenter/17846.bmp I/EXTERNAL_CONTENT_URI: _id : 36473 , _data : /storage/sdcard0/Backucup/com.UCMobile/homepage/appcenter/19971.bmp I/EXTERNAL_CONTENT_URI: _id : 36474 , _data : /storage/sdcard0/Backucup/com.UCMobile/homepage/appcenter/19972.bmp I/EXTERNAL_CONTENT_URI: _id : 36475 , _data : /storage/sdcard0/Backucup/com.UCMobile/homepage/appcenter/19973.bmp I/EXTERNAL_CONTENT_URI: _id : 36476 , _data : /storage/sdcard0/Backucup/com.UCMobile/homepage/appcenter/20068.bmp I/EXTERNAL_CONTENT_URI: _id : 36477 , _data : /storage/sdcard0/Backucup/com.UCMobile/homepage/appcenter/22123.bmp I/EXTERNAL_CONTENT_URI: _id : 36924 , _data : /storage/sdcard0/sina/weibo/weibo/img-7b5bb4b9104581089705450942ebaf17.gif
最后你就可以拿着这些地址胡作为非了,赶快根据这种思路去实现一下获取音频,视频文件的功能吧!