Android内容提供器——ContentProvider
来源:互联网 发布:流程 数据库设计 编辑:程序博客网 时间:2024/04/24 05:06
要想访问内容提供器中的数据,就一定要借助ContentResolver类。
可以通过中Context的getContentResolver()方法来获取该类的实例。
不同于SQLiteDatabase,ContentResolver中的增删改查都是不接收表名参数的,而是使用Uri参数代替,它被称之为内容URI。
URI由两部分组成: authority和path。
authority用于区分不同程序,一般以程序包名进行命名。
com.example.app –> com.example.app.provider
path则用于同一程序的不同的表,通常在authority之后。
数据库中 table1,table1 –> /table1, /table2
内容URI标准格式如下:
content://com.example.app.provider/table1
内容URI清晰地表达了要访问哪个应用程序中的哪张表,如果使用表名,系统无法得知我们期望访问哪个程序。
内容URI字符串解析为Uri对象,才可以作为参数传入
Uri uri =Uri.parse("content://com.example.app.provider/table1");
Cursor cursor = getContentResolver().query(uri,projection, selection,selectionArgs,sortOrder);
与SQLiteDatabase中query对照表
query()方法参数 对应SQL部分 描述
uri from table_name 指定表
projection select column1,column2 指定列
selection where column =value 指定where约束条件
访问本地相册的写法:
private static final int CHOOSE_PHOTO = 2;private void openAlbum() { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); //设置类型 startActivityForResult(intent, CHOOSE_PHOTO); }
@TargetApi(19) private void handleImageOnKitKat(Intent data) { String imagePath = null; Uri uri = data.getData(); //4.4以上版本返回封装的图片 uri // 这个 uri 有三种解析形式 if (DocumentsContract.isDocumentUri(this, uri)) { //如果是 document 类型的 uri,则通过 document id 解析 String docId = DocumentsContract.getDocumentId(uri); if ("com.android.providers.media.documents".equals(uri.getAuthority())) { String id = docId.split(":")[1]; String selection = MediaStore.Images.Media._ID + "=" + id; imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection); } else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) { Uri contentUri = ContentUris.withAppendedId( Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId)); imagePath = getImagePath(contentUri, null); } } else if ("content".equalsIgnoreCase(uri.getScheme())) { //如果是 content 类型的 uri , 则使用普通方法处理 imagePath = getImagePath(uri, null); } else if ("file".equalsIgnoreCase(uri.getScheme())) { imagePath = uri.getPath(); } displayImage(imagePath); } private void handleImageBeforeKitKat(Intent data) { Uri uri = data.getData(); String imagePath = getImagePath(uri, null); displayImage(imagePath); } private String getImagePath(Uri uri, String selection) { String path = null; // 通过Uri和selection来获取真实的图片路径 Cursor cursor = getContentResolver().query(uri, null, selection, null, null); if (cursor != null) { if (cursor.moveToFirst()) { path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)); } cursor.close(); } return path; }
程序调试截图:
- Android内容提供器——ContentProvider
- Android 内容提供器 ContentProvider
- Android 组件ContentProvider(一)内容提供器
- [Android四大组件] ContentProvider 内容提供器
- ContentProvider ---内容提供器
- ContentProvider内容提供器
- ContentProvider(内容提供器)
- ContentProvider 内容提供器
- ContentProvider--------详解内容提供器
- Android ContentProvider(内容提供器运用一)
- Android之内容提供器ContentProvider的简单表示
- Android之ContentProvider(一):内容提供器的用法
- Android 内容提供器---创建内容提供器(实现ContentProvider类)
- Android 内容提供器---创建内容提供器(实现ContentProvider的MIME类型)
- Android 内容提供器---创建内容提供器(实现ContentProvider类)
- Android 内容提供器---创建内容提供器(实现ContentProvider的MIME类型)
- 跨程序内容提供器ContentProvider
- 创建自己的内容提供器ContentProvider
- 获取hive表最新分区(准实时算法)
- 硬件检测:安装、停用
- 二分图总结
- C++11 知识图导览
- 编写更少量的代码:使用apache commons工具类库
- Android内容提供器——ContentProvider
- 关于子查询中使用limit
- stm32f103 rtc 获取 日历 时钟
- java基础day03
- Android 开发之 Kotlin 初始篇
- python-技术指标-ema算法
- Json解析的四种方法
- 如何将内存图像数据封装成QImage
- 17-JSCPC&&CCPC中南赛区 H-Highway 树的直径DFS