一、多媒体数据库与ContentResovler:
Android提供的多媒体数据库MediaStore保存了音频、视频、图片的全部基本信息,
保存在/data/data/com.android.providers.media/databases下的external.db数据库
事实上以下URI是ContentProvider给我们封装了用来访问系统数据库的入口
二、ContentResobler相关类比
- ContentProvider相当于服务器,将系统的数据库访问接口URI给我们;
- ContentResolver相当于浏览器,输入URI和相应的条件,就可以访问数据;
- 每个URI都对应着一个相关数据表,使用ContentResolver相关方法进行表查询;
- ContentResolver更像是SQLiteDatebase类,有查询,删除等相关方法;
三、一些常用的URI(表):
- 音乐URI(表)
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI=content://media/external/audio/media - 专辑URI(表)
MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI=//content://media/external/audio/albums - 图片URI(表)
MediaStore.Images.Media.EXTERNAL_CONTENT_URI=content://media/external/images/media - 视频URI(表)
MediaStore.Video.Media.EXTERNAL_CONTENT_URI=content://media/external/video/media
除了媒体库,访问通讯录时,使用通讯录数据库 - 通讯录URI(表)
ContactsContract.Contacts.CONTENT_URI=content://com.android.contacts/contacts - 电话URI(表) 取得电话号
ContactsContract.CommonDataKinds.Phone.CONTENT_URI=content://com.android.contacts/data/phones
在Activity中获取ContentResolver对象,并在AndroidManifest.xml加上访问外部数据库权限
ContentResolver resolver=getContentResolver()
四、对URI操作,取数据
以下java代码都在Activity中书写,含有View对象参数的为Button的Click点击事件
(一)、访问sd卡的图片
获取图片相关信息,使用存放图片的URI(表)存放
public void getImage(View view){
ContentResolver resolver=getContentResolver();
Uri imageUri=MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String imageName=MediaStore.Images.Media.DISPLAY_NAME;
String path=MediaStore.Images.Media.DATA;
String dirName= MediaStore.Images.Media.BUCKET_DISPLAY_NAME;//存储文件夹的名字
String id=MediaStore.Images.Media._ID;
Cursor cursor=resolver.query(imageUri,new String[]{imageName,path,dirName,id},null,null,null,null);
while(cursor.moveToNext()){
Log.i("dayang","imageName:"+cursor.getString(cursor.getColumnIndex(imageName)));
Log.i("dayang","path:"+cursor.getString(cursor.getColumnIndex(path)));
Log.i("dayang","dirName:"+cursor.getString(cursor.getColumnIndex(dirName)));
String image_id=cursor.getString(cursor.getColumnIndex(id));
Log.i("dayang","id:"+image_id);
Log.i("dayang","缩略图存放路径path:"+getThumbnail(image_id));
}
}
获取图片缩略图相关信息,使用图片缩略图的URI(表)
private String getThumbnail(String id){
ContentResolver resolver=getContentResolver();
Uri imageUri=MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI;
String path=MediaStore.Images.Thumbnails.DATA;
String image_id=MediaStore.Images.Thumbnails.IMAGE_ID;
Cursor cursor=resolver.query(imageUri,new String[]{path},image_id+"=?",new String[]{id},null);
String str;
while(cursor.moveToNext()){
str=cursor.getString(cursor.getColumnIndex(path));
return str;
}
return null;
}
(二)、访问sd卡的音频
获取音乐的相关信息,使用音乐URI(表)
public void getMusic(View view){
ContentResolver resolver=getContentResolver();
Uri musicUri=MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String musicName=MediaStore.Audio.Media.TITLE;
String musicFileName=MediaStore.Audio.Media.DISPLAY_NAME;
String artist=MediaStore.Audio.Media.ARTIST;
String album=MediaStore.Audio.Media.ALBUM;
String path=MediaStore.Audio.Media.DATA;
String album_id=MediaStore.Audio.Media.ALBUM_ID;
Cursor cursor=resolver.query(musicUri,new String[]{musicFileName,musicName,artist,album,path,album_id},null,null,null,null);
while(cursor.moveToNext()){
Log.i("dayang","带文件后缀,musicFileName:"+cursor.getString(cursor.getColumnIndex(musicFileName)));
Log.i("dayang","musicName:"+cursor.getString(cursor.getColumnIndex(musicName)));
Log.i("dayang","artist:"+cursor.getString(cursor.getColumnIndex(album)));
Log.i("dayang","path:"+cursor.getString(cursor.getColumnIndex(path)));
Log.i("dayang","album:"+cursor.getString(cursor.getColumnIndex(album)));
String id=cursor.getString(cursor.getColumnIndex(album_id));
Log.i("dayang","album_id"+id);
Log.i("dayang","album_thumbnial"+getAlbumThumbnail(id));//获取缩略图
}
}
获取专辑缩略图,使用专辑URI(表)
/**
* 得到专辑缩略图
* @return
*/
public String getAlbumThumbnail(String album_id){
ContentResolver resolver=getContentResolver();
Uri albumUri=MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
String thumbnail=MediaStore.Audio.Albums.ALBUM_ART;
String id=MediaStore.Audio.Albums._ID;
String str=null;
Cursor cursor=resolver.query(albumUri,new String[]{thumbnail},id+"=?",new String[]{album_id},null);
while(cursor.moveToNext()){
str=cursor.getString(cursor.getColumnIndex(thumbnail));
}
return str;
}
(三)、访问sd卡的视频,使用视频URI(表)
public void getVideo(View view){
ContentResolver resolver=getContentResolver();
Uri videoUri=MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
String videoName=MediaStore.Video.Media.DISPLAY_NAME;
String videoArtist=MediaStore.Video.Media.ARTIST;
String path=MediaStore.Video.Media.DATA;
String parentFile=MediaStore.Video.Media.BUCKET_DISPLAY_NAME;
Cursor cursor=resolver.query(videoUri,new String[]{videoName,videoArtist,path,parentFile},null,null,null);
while(cursor.moveToNext()){
Log.i("dayang","videoName"+cursor.getString(cursor.getColumnIndex(videoName)));
Log.i("dayang","path"+cursor.getString(cursor.getColumnIndex(videoArtist)));
Log.i("dayang","parentFile"+cursor.getString(cursor.getColumnIndex(parentFile)));
Log.i("dayang","videoArtist"+cursor.getString(cursor.getColumnIndex(videoArtist)));
}
}
(四)、获取通讯录,使用通讯录URI(表)
访问通讯录数据库,在AndroidManifest.xml加上访问通讯录权限
获取通讯录中联系人名字,相应id
public void getContact(View view){
ContentResolver resolver=getContentResolver();
Uri contactUri= ContactsContract.Contacts.CONTENT_URI;
String name=ContactsContract.Contacts.DISPLAY_NAME;
String id=ContactsContract.Contacts._ID;
Cursor cursor=resolver.query(contactUri,new String[]{name,id},null,null,null,null);
while(cursor.moveToNext()){
Log.i("dayang","name:"+cursor.getString(cursor.getColumnIndex(name)));
String contact_id=cursor.getString(cursor.getColumnIndex(id));
Log.i("dayang","id:"+contact_id);
Log.i("dayang","number:"+getNumber(contact_id));//调用getNumber方法
}
}
获取通讯录中联系人电话号
public String getNumber(String id){
ContentResolver resolver=getContentResolver();
Uri numberUri=ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
Log.i("dayang",numberUri+"");
String number=ContactsContract.CommonDataKinds.Phone.NUMBER;
String nickname=ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME;
String contact_id=ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
Cursor cursor=resolver.query(numberUri,new String[]{number,nickname},contact_id+"=?",new String[]{id},null);
String str=null;
while(cursor.moveToNext()){
str=cursor.getString(cursor.getColumnIndex(number));
Log.i("dayang","nickname"+cursor.getString(cursor.getColumnIndex(nickname)));
return str;
}
return str;
}