这篇文章最核心的就是去学习如何学习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

好,查询方法的简要说明如上,这里我需要使用的最关键的参数就是Uri 了,你可能会有疑问,天知道这个参数该是什么,别担心,会有办法的。

我们先打开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).

这是什么意思呢?也就是说,我们有了Android提供给我们的Uri地址,公开的,这样,我们就可以根据这里提供的Uri地址去查找系统中的联系人,以及它们的详细信息。同样的,我们也可以获取系统中提供其它信息,比如系统中的图片,我们举一反三来试炼一下:

刚才我们点开的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  

这里有音频,有图片,有视频。好,我们仅获取图片就OK,点开 MediaStore.Images的链接,这里仅仅有两个实现类:

 
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  

我们选择 MediaStore.Images.Media,这里赫然有两个属性:

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 

最后你就可以拿着这些地址胡作为非了,赶快根据这种思路去实现一下获取音频,视频文件的功能吧!

Android中使用官方提供好的功能使用说明(比如系统图库获取),也作为延生学习的学习文档相关推荐

  1. android筛选功能代码,Android中 TeaScreenPopupWindow多类型筛选弹框功能的实例代码

    Github地址 YangsBryant/TeaScreenPopupWindow (Github排版比较好,建议进入这里查看详情,如果觉得好,点个star吧!) 引入module allprojec ...

  2. android图片美化开源,GitHub - xingxing-yan/BLImage: Android中美化图片的库。功能包括滤镜,贴纸,标签,裁剪,涂鸦,亮度,饱和度,对比度,马赛克等功能...

    BLImage Android中美化图片的库.功能包括滤镜,贴纸,标签,裁剪,涂鸦,亮度,饱和度,对比度,马赛克等功能 效果图 添加依赖: 在project的build.gradle中添加: allp ...

  3. 如何在Python中将数据插入到Word模板中生成一份Word文档

    在一些的项目开发中,会有一些生成Word文件的操作,比如将获取到的一些数据添加到Word模板当中的相应的位置生成一份Word文档. 由于最近的Python项目当中需要将一些从服务器查出的数据添加到Wo ...

  4. Python爬虫高级库之一的lxml库中,ET.parse()是一个非常重要的方法。它可以将任意XML或HTML格式的文档解析成一个ElementTree对象,...

    Python爬虫高级库之一的lxml库中,ET.parse()是一个非常重要的方法.它可以将任意XML或HTML格式的文档解析成一个ElementTree对象,方便我们对结构化的数据进行处理和分析.在 ...

  5. android 8.0官方刷机包,安卓8.0系统

    安卓8.0系统于近日正式开放下载开发者预览版,官方代号又叫做Android O,是下一版本的 Android 系统,它优化了电源及性能,并提供了多种全新方式来扩展您的应用.比如安卓8.0系统引入了通知 ...

  6. office中正则表达式、定位对象、导航栏、拆分单元格、通过关键字筛选、替换(文档格式、内容等)

    提供一个思路,可以将pdf转化为word,word直接贴到excell表格中,大致用到了标题所示技巧,可以直接将一整个pdf中相一直同字段中的内容提取到excell中.前期一直在将word中的一段段条 ...

  7. Django框架深入了解_05 (Django中的缓存、Django解决跨域流程(非简单请求,简单请求)、自动生成接口文档)(一)

    阅读目录 一.Django中的缓存: 前戏: Django中的几种缓存方式: Django中的缓存应用: 二.跨域: 跨域知识介绍: CORS请求分类(简单请求和非简单请求) 示例: 三.自动生成接口 ...

  8. Django框架深入了解_05 (Django中的缓存、Django解决跨域流程(非简单请求,简单请求)、自动生成接口文档)(二)

    二.跨域: 回到顶部 跨域知识介绍: 点我以前博客 跨域解决方法:CORS:跨域资源共享 CORS请求分类(简单请求和非简单请求) 简单请求(simple request):只需要在头信息之中增加一个 ...

  9. Android调用系统图库获取图片

    1.首先来到图库按钮,在定义图库按钮之前,首先定义三个File文件对象,用来存放图片的截取,图片的选择图片,拍照的图片.由于需要进行图片的上传,需要进行图片的加载层,getCacheDiar()项目A ...

最新文章

  1. 【组队学习】【29期】1. 编程实践(数据可视化)
  2. Windows Server 8 Beta 初体验之三 Active Directory
  3. python可以自学吗需要什么基础-python自学行吗?给编程初学者零基础入门的建议...
  4. linux基础(2)-网卡配置
  5. Windows 技术篇-设置dns提升网速,刷新dns缓存
  6. 【扫盲】什么是FTP、FTPS 和 SFTP?
  7. access开发精要(5)-合计group by
  8. android 请求网络异步载入
  9. 推荐系统可利用的特征
  10. linux texmaker编译,在Ubuntu下安装和编译LaTex
  11. 基于SpringBoot,来实现MySQL读写分离技术
  12. python编程入门与案例详解-Pythony运维入门之Socket网络编程详解
  13. [转载] OpenCV-Python图像位与运算bitwise_and函数详解
  14. Study From Work(2011-3-2)
  15. “狼牙抓鸡”现身IT江湖
  16. PLM Agile 随笔
  17. python练手经典100例项目-Python 的练手项目有哪些值得推荐?
  18. 操作系统—CPU调度与内存管理
  19. Spring Boot + Spring-Security实现前后端分离双重身份认证初学者指南(手机号密码JWT + 短信验证码)
  20. Linux攻关之基础模块十 特殊权限

热门文章

  1. mysql模糊查询与预编译_mysql预编译模糊查询恶心了我一天的时间,终于弄好了。但是还有一点不明白。如下:...
  2. 如何把一个网页生成一个快捷方式在桌面?_如何打造一个简洁、高效的桌面?
  3. long类型python_Python类型long vs C'long long'
  4. selenium firefox驱动_Python3+selenium配置常见报错解决方案
  5. 1 0.99999的悖论_无限小数与芝诺悖论
  6. Vue3 高级语法(一)—— h函数、jsx
  7. JavaScript高级之ECMAScript 6 新特性
  8. python web开发 JavaScript基础
  9. WIN 10 安装 Hadoop 2.7.7 + Spark 2.4.7 记录
  10. LeetCode 1064. 不动点(二分查找)