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

这篇文章最核心的就是去学习如何学习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 
最后你就可以拿着这些地址胡作为非了,赶快根据这种思路去实现一下获取音频,视频文件的功能吧!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/479461.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

再介绍一篇Contrastive Self-supervised Learning综述论文

文 | 黄浴源 | 知乎之前已经介绍过三篇自监督学习的综述&#xff1a;《怎样缓解灾难性遗忘&#xff1f;持续学习最新综述三篇&#xff01;》。这是最近2020年10月arXiv上的又一篇论文"A Survey On Contrastive Self-supervised Learning"。论文地址&#xff1a;https…

GCN-Based User Representation Learning for Unifying Robust Recommendation and Fraudster Detection

GCN-Based User Representation Learning for Unifying Robust Recommendation and Fraudster Detection 点击率预测&#xff1a;其主要思想是根据用户的历史行为对一组未评级的项目进行评级预测&#xff0c;然后从预测评级最高的项目中选择个性化推荐。 欺诈检测&#xff1a;…

公开课 | 知识图谱构建与应用概述

本文转载自公众号&#xff1a;博文视点Broadview。 AI是新的生产力&#xff0c;知识图谱是AI进步的阶梯。随着近年来人工智能的进一步发展&#xff0c;知识图谱也取得了一系列新的进展&#xff0c;并在各个行业中落地应用。知识图谱的相关技术已经在搜索引擎、智能问答、…

python中mysql更新字段中传参问题

更新表 可以使用“UPDATE”语句&#xff0c;更新表格内的现有记录: 示例 将地址栏由“Valley 345”改写为“Canyoun 123”: import mysql.connectormydb mysql.connector.connect(host"localhost",user"你的用户名",passwd"你的密码",databa…

LeetCode 217. 存在重复元素(哈希)

文章目录1. 题目2. 解题1. 题目 给定一个整数数组&#xff0c;判断是否存在重复元素。 如果任何值在数组中出现至少两次&#xff0c;函数返回 true。如果数组中每个元素都不相同&#xff0c;则返回 false。 示例 1:输入: [1,2,3,1] 输出: true 示例 2:输入: [1,2,3,4] 输出:…

美团BERT的探索和实践

2018年&#xff0c;自然语言处理&#xff08;Natural Language Processing&#xff0c;NLP&#xff09;领域最激动人心的进展莫过于预训练语言模型&#xff0c;包括基于RNN的ELMo[1]和ULMFiT[2]&#xff0c;基于Transformer[3]的OpenAI GPT[4]及Google BERT[5]等。下图1回顾了近…

解决AndroidStudio添加ProjectLibary后在编译时遇到的各种问题之解决方式索引(finished with non-zero exit value and so on...)

解决AndroidStudio添加ProjectLibary后在编译时遇到的各种问题之解决方式索引(finished with non-zero exit value and so on...)因为项目需要&#xff0c;我需要将一个外部工程作为Libary导入项目&#xff0c;起初导入还是比较简单的&#xff0c;但是在编译的时候就遇到了各种…

论文浅尝 | 探索将预训练语言模型用于事件抽取和事件生成

论文笔记整理&#xff1a;郝凯龙&#xff0c;南京大学硕士链接&#xff1a;https://www.aclweb.org/anthology/P19-1522.pdf动机传统的 ACE 事件抽取任务依赖于人工标注的数据&#xff0c;耗费大量的人力并且数据量有限&#xff0c;数据量不足给事件抽取带来了阻碍。传统的事件…

谷歌、CMU发文:别压榨单模型了!集成+级联上分效率更高!

文 | Sherry 不是小哀集成模型&#xff08;Ensemble&#xff09;可以提升模型的精度&#xff0c;但往往面临提升计算量的困境&#xff0c;用级联模型&#xff08;Cascade&#xff09;在预测时提前中断则可解决计算量的问题。最近&#xff0c;谷歌和CMU的研究者对此进行了深入的…

Linux-Centos 安装Anaconda(2021)

安装步骤 1、下载Anaconda到本地 Anaconda3-5.3.1-Linux-x86_64.sh2、上传本地 Anaconda 文件到 linux 服务器 rz # 打开笔记本方式上传3、安装 bash Anaconda3-5.3.1-Linux-x86_64.sh4、检查是否安装成功 conda info --envs配置步骤 如果没有安装成功&#xff0c;需要…

大数据技术和python开发工程师

一&#xff1a;大数据技术 简单来说&#xff0c;从大数据的生命周期来看&#xff0c;无外乎四个方面&#xff1a;大数据采集、大数据预处理、大数据存储、大数据分析&#xff0c;共同组成了大数据生命周期里最核心的技术&#xff0c;下面分开来说&#xff1a;一、大数据采集大数…

LeetCode 219. 存在重复元素 II(哈希)

文章目录1. 题目2. 解题1. 题目 给定数组nums和常数k&#xff0c;存在不同的i、j使得nums[i] nums[j]&#xff0c;且abs(i-j) < k。 输入: nums [1,2,3,1], k 3 输出: true 示例 2:输入: nums [1,0,1,1], k 1 输出: true 示例 3:输入: nums [1,2,3,1,2,3], k 2 输出…

记录对String.format(Formatter().format())方法的总结

String.format其实是调用的Formatter.format&#xff1a; public static String format(String format, Object... args) {return new Formatter().format(format, args).toString();}第一个参数是格式化字符串&#xff0c;第二个参数是可变的被格式化的参数。 我们主要看的是格…

Android静态代码扫描效率优化与实践

背景与问题 DevOps实践中&#xff0c;我们在CI(Continuous Integration)持续集成过程主要包含了代码提交、静态检测、单元测试、编译打包环节。其中静态代码检测可以在编码规范&#xff0c;代码缺陷&#xff0c;性能等问题上提前预知&#xff0c;从而保证项目的交付质量。Andro…

还在用[CLS]?从BERT得到最强句子Embedding的打开方式!

文&#xff1a;涅生编&#xff1a;兔子酱你有尝试从 BERT 提取编码后的 sentence embedding 吗&#xff1f;很多小伙伴的第一反应是&#xff1a;不就是直接取顶层的[CLS] token的embedding作为句子表示嘛&#xff0c;难道还有其他套路不成&#xff1f;nono&#xff0c;你知道这…

论文浅尝 | BERT:Pre-training of Deep Bidirectional Transformers

论文笔记整理&#xff1a;王春培&#xff0c;天津大学硕士。链接&#xff1a;https://arxiv.org/pdf/1810.04805.pdf动机将预训练语言表示应用于下有任务现有两种策略&#xff1a;基于特征的和基于微调的。文章认为当前技术限制了预训练的能力&#xff0c;尤其是基于微调的方法…

欺诈检测相关论文

欺诈检测相关论文一、分类1、GEM2、HACUD3、MAHINDER4、Semi-GNN5、MvMoE6、AMG-DP7、AddGraph8、NetWalk9、DOMINANT10、GraphConsis11、PC-GNN12、TRUST二、类别不平衡一、分类 1、GEM 来自蚂蚁金服的论文&#xff0c;他们提出GEM模型&#xff0c;是一个异质图神经网络方法&a…

LeetCode 220. 存在重复元素 III(lower_bound)

文章目录1. 题目2. 解题1. 题目 给定一个整数数组&#xff0c;判断数组中是否有两个不同的索引 i 和 j&#xff0c;使得 nums [i] 和 nums [j] 的差的绝对值最大为 t&#xff0c;并且 i 和 j 之间的差的绝对值最大为 ķ。 示例 1:输入: nums [1,2,3,1], k 3, t 0 输出: tr…

Android自定义控件入门实践之雷达扫描控件

以前因为工作的关系&#xff0c;对于自定义控件用的少之又少&#xff0c;无非就是把几个控件放置到ViewGroup内部&#xff0c;然后提供开放方法&#xff0c;就成了一个所谓的自定义控件&#xff0c;但是这种小伎俩太简单&#xff0c;面试的时候这点东西根本Hold不住场&#xff…

linux不挂断运行python文件

nohup命令及其输出文件 今天在linux上部署wdt程序&#xff0c;在SSH客户端执行./start-dishi.sh,启动成功,在关闭SSH客户端后&#xff0c;运行的程序也同时终止了&#xff0c;怎样才能保证在推出SSH客户端后程序能一直执行呢&#xff1f;通过网上查找资料&#xff0c;发现需要…