Android内容提供器——ContentProvider

来源:互联网 发布:流程 数据库设计 编辑:程序博客网 时间:2024/04/24 05:06

 
  要想访问内容提供器中的数据,就一定要借助ContentResolver类。
可以通过中ContextgetContentResolver()方法来获取该类的实例。

  不同于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;    }

  程序调试截图:
     
     
     这里写图片描述
     这里写图片描述
     这里写图片描述

原创粉丝点击