Android查询手机媒体库
前言
最近项目中需要查询本地音乐进行展示播放,固有此篇文章。
什么是MediaStore?
MediaStore是android中的多媒体数据库,android中多媒体信息都可以从这里提取。这个MediaStore包括了多媒体数据库的所有信息,包括 音频,视频和图像,android把所有的多媒体数据库接口进行了封装,所有的数据库不用自己进行创建,直接调用利用ContentResolver去掉 用那些封装好的接口就可以进行数据库的操作了。当需要进行本地图片、视频、音乐的元数据查询时可以使用该类。
MediaStore内嵌类
MediaStore.Audio
音频内容容器
MediaStore.Images
图片内容容器
MediaStore.Video
视频内容容器
MediaStore.Files
MediaProvider表包含媒体存储中所有文件的索引,包括非媒体文件
读取图片
MediaStore.Images.Media.EXTERNAL_CONTENT_URI 查询所有的图片
常用字段如下
MediaStore.Images.Media._ID 图片id
MediaStore.Images.Media.DISPLAY_NAME 图片文件名
MediaStore.Images.Media.DATA 图片保存全路径
MediaStore.Images.Media.WIDTH 图片宽度
MediaStore.Images.Media.HEIGHT 图片高度
MediaStore.Images.Media.DATE_ADDED 图片创建时间
MediaStore.Images.Media.DATE_MODIFIED 图片最后修改时间
MediaStore.Images.Media.SIZE 图片大小
代码如下:
PicActivity:
package com.zhqy.mediastoredemo;
import android.database.Cursor;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.zhqy.mediastoredemo.adapter.AudioAdapter;
import com.zhqy.mediastoredemo.adapter.PictureAdapter;
import java.util.ArrayList;
import java.util.List;
import domain.Picture;
import domain.Song;
public class PicActivity extends AppCompatActivity {
List<Picture> pics;
RecyclerView rv;
PictureAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audio);
initView();
initData();
}
public void initView(){
rv=findViewById(R.id.rc);
rv.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
adapter=new PictureAdapter(this);
}
public void initData(){
pics=new ArrayList<>();
/**
* MediaStore.Images.Media.EXTERNAL_CONTENT_URI 查询所有的图片
* MediaStore.Images.Media._ID 图片id
* MediaStore.Images.Media.DISPLAY_NAME 图片文件名
* MediaStore.Images.Media.DATA 图片保存全路径
* MediaStore.Images.Media.WIDTH 图片宽度
* MediaStore.Images.Media.HEIGHT 图片高度
* MediaStore.Images.Media.DATE_ADDED 图片创建时间
* MediaStore.Images.Media.DATE_MODIFIED 图片最后修改时间
* MediaStore.Images.Media.SIZE 图片大小
*/
String[] projection=new String[]{MediaStore.Images.Media._ID,MediaStore.Images.Media.DISPLAY_NAME,MediaStore.Images.Media.DATA
,MediaStore.Images.Media.WIDTH, MediaStore.Images.Media.HEIGHT, MediaStore.Images.Media.DATE_ADDED,MediaStore.Images.Media.DATE_MODIFIED
,MediaStore.Images.Media.SIZE
};
Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, MediaStore.Images.Media.DEFAULT_SORT_ORDER);
while (cursor!=null&&cursor.moveToNext()){
long picId=cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media._ID));
String name = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME));
String data = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
long width = cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media.WIDTH));
long height=cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media.HEIGHT));
long addTime = cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media.DATE_ADDED));
long modifyTime = cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED));
long size=cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media.SIZE ));
Picture picture=new Picture();
picture.setData(data);
picture.setWidth(width);
picture.setHeight(height);
picture.setName(name);
picture.setPicCreateTime(addTime);
picture.setPicId(picId);
picture.setPicModifyTime(modifyTime);
picture.setSize(size);
pics.add(picture);
}
adapter.setList(pics);
rv.setAdapter(adapter);
}
}
PictureAdapter:
package com.zhqy.mediastoredemo.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.zhqy.mediastoredemo.R;
import java.util.List;
import domain.Picture;
import domain.Song;
/**
* Created by jackal on 2019/10/10.
*/
public class PictureAdapter extends RecyclerView.Adapter<PictureAdapter.AudioViewHolder> {
List<Picture> list;
Context context;
public PictureAdapter(Context context){
this.context=context;
}
@Override
public AudioViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View inflate = LayoutInflater.from(context).inflate(R.layout.item_audio, parent, false);
AudioViewHolder viewHolder=new AudioViewHolder(inflate);
viewHolder.tv=inflate.findViewById(R.id.tv);
return viewHolder;
}
@Override
public void onBindViewHolder(AudioViewHolder holder, int position) {
Picture pic = list.get(position);
holder.tv.setText(pic.toString());
}
@Override
public int getItemCount() {
return list.size();
}
class AudioViewHolder extends RecyclerView.ViewHolder{
TextView tv;
public AudioViewHolder(View itemView) {
super(itemView);
}
}
public void setList(List<Picture> list) {
this.list = list;
}
}
测试结果
读取音频
常用查询字段
MediaStore.Audio.AudioColumns.ALBUM 专辑名称
MediaStore.Audio.AudioColumns.ALBUM_ID 所在专辑表中的id
MediaStore.Audio.AudioColumns.ARTIST 艺术家名称
MediaStore.Audio.AudioColumns.ARTIST_ID 所在艺术家表中的id
MediaStore.Audio.AudioColumns.DURATION 音频时长
MediaStore.Audio.AudioColumns.IS_MUSIC 是否是音乐文件
MediaStore.Audio.AudioColumns.DATA 文件保存全路径
MediaStore.Audio.Media.DISPLAY_NAME 歌曲文件名
MediaStore.Audio.Media.TITLE 歌名
代码如下:
AudioActivity:
package com.zhqy.mediastoredemo;
import android.database.Cursor;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.zhqy.mediastoredemo.adapter.AudioAdapter;
import java.util.ArrayList;
import java.util.List;
import domain.Song;
public class AudioActivity extends AppCompatActivity {
List<Song> songs;
RecyclerView rv;
AudioAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audio);
initView();
initData();
}
public void initView(){
rv=findViewById(R.id.rc);
rv.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
adapter=new AudioAdapter(this);
}
public void initData(){
songs=new ArrayList<>();
//查询那些字段?
/**
* MediaStore.Audio.AudioColumns.ALBUM 专辑名称
* MediaStore.Audio.AudioColumns.ALBUM_ID 所在专辑表中的id
* MediaStore.Audio.AudioColumns.ARTIST 艺术家名称
* MediaStore.Audio.AudioColumns.ARTIST_ID 所在艺术家表中的id
* MediaStore.Audio.AudioColumns.DURATION 音频时长
* MediaStore.Audio.AudioColumns.IS_MUSIC 是否是音乐文件
* MediaStore.Audio.AudioColumns.DATA 文件保存全路径
* MediaStore.Audio.Media.DISPLAY_NAME 歌曲文件名
* MediaStore.Audio.Media.TITLE 歌名
* ...等等
*/
String[] projections=new String[]{MediaStore.Audio.AudioColumns.ALBUM,MediaStore.Audio.AudioColumns.ALBUM_ID,MediaStore.Audio.AudioColumns.ARTIST
,MediaStore.Audio.AudioColumns.ARTIST_ID,MediaStore.Audio.AudioColumns.DURATION,MediaStore.Audio.AudioColumns.IS_MUSIC,MediaStore.Audio.AudioColumns.DATA
, MediaStore.Audio.Media.DISPLAY_NAME,MediaStore.Audio.Media.TITLE};
//MediaStore.Audio.Media.EXTERNAL_CONTENT_URI 保存所有歌曲的信息
//MediaStore.Audio.Media.DEFAULT_SORT_ORDER 默认排序
Cursor query = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,projections,MediaStore.Audio.AudioColumns.IS_MUSIC+"!=0",null,MediaStore.Audio.Media.DEFAULT_SORT_ORDER );
while (query!=null&&query.moveToNext()){
String album = query.getString(query.getColumnIndex(MediaStore.Audio.AudioColumns.ALBUM));
long album_id = query.getLong(query.getColumnIndex(MediaStore.Audio.AudioColumns.ALBUM_ID));
String artist = query.getString(query.getColumnIndex(MediaStore.Audio.AudioColumns.ARTIST));
long artist_id = query.getLong(query.getColumnIndex(MediaStore.Audio.AudioColumns.ARTIST_ID));
long duration = query.getLong(query.getColumnIndex(MediaStore.Audio.AudioColumns.DURATION));
String data = query.getString(query.getColumnIndex(MediaStore.Audio.AudioColumns.DATA));
String title=query.getString(query.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
String songName=query.getString(query.getColumnIndex(MediaStore.Audio.Media.TITLE));
Song song=new Song();
song.setAlbum(album);
song.setAlbum_id(album_id);
song.setArtist(artist);
song.setArtist_id(artist_id);
song.setDuration(duration);
song.setData(data);
song.setTitle(title);
song.setSongName(songName);
songs.add(song);
}
adapter.setList(songs);
rv.setAdapter(adapter);
}
}
AudioAdapter:
package com.zhqy.mediastoredemo.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.zhqy.mediastoredemo.R;
import java.util.List;
import domain.Song;
/**
* Created by jackal on 2019/10/10.
*/
public class AudioAdapter extends RecyclerView.Adapter<AudioAdapter.AudioViewHolder> {
List<Song> list;
Context context;
public AudioAdapter(Context context){
this.context=context;
}
@Override
public AudioViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View inflate = LayoutInflater.from(context).inflate(R.layout.item_audio, parent, false);
AudioViewHolder viewHolder=new AudioViewHolder(inflate);
viewHolder.tv=inflate.findViewById(R.id.tv);
return viewHolder;
}
@Override
public void onBindViewHolder(AudioViewHolder holder, int position) {
Song song = list.get(position);
holder.tv.setText(song.toString());
}
@Override
public int getItemCount() {
return list.size();
}
class AudioViewHolder extends RecyclerView.ViewHolder{
TextView tv;
public AudioViewHolder(View itemView) {
super(itemView);
}
}
public void setList(List<Song> list) {
this.list = list;
}
}
测试结果
读取音频
常见字段
MediaStore.Video.Media.EXTERNAL_CONTENT_URI 查询所有的音频
MediaStore.Video.Media._ID 视频ID
MediaStore.Video.Media.ALBUM 专辑
MediaStore.Video.Media.ARTIST 艺术家姓名
MediaStore.Video.Media.DATE_ADDED 视频文件创建时间
MediaStore.Video.Media.DISPLAY_NAME 视频文件名称
MediaStore.Video.Media.TITLE 视频名
MediaStore.Video.Media.DURATION 视频时长
MediaStore.Video.Media.SIZE 视频大小
MediaStore.Video.Media.DATA 视频保存路径
MediaStore.Video.Media.DATE_MODIFIED 视频文件修改时间
代码如下:
VideoActivity:
package com.zhqy.mediastoredemo;
import android.database.Cursor;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.zhqy.mediastoredemo.adapter.AudioAdapter;
import com.zhqy.mediastoredemo.adapter.VideoAdapter;
import java.util.ArrayList;
import java.util.List;
import domain.Song;
import domain.Video;
public class VideoActivity extends AppCompatActivity {
List<Video> videos;
RecyclerView rv;
VideoAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audio);
initView();
initData();
}
public void initView() {
rv = findViewById(R.id.rc);
rv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
adapter = new VideoAdapter(this);
}
public void initData() {
videos = new ArrayList<>();
/**
* MediaStore.Video.Media.EXTERNAL_CONTENT_URI 查询所有的音频
* MediaStore.Video.Media._ID 视频ID
* MediaStore.Video.Media.ALBUM 专辑
* MediaStore.Video.Media.ARTIST 艺术家姓名
* MediaStore.Video.Media.DATE_ADDED 视频文件创建时间
* MediaStore.Video.Media.DISPLAY_NAME 视频文件名称
* MediaStore.Video.Media.TITLE 视频名
* MediaStore.Video.Media.DURATION 视频时长
* MediaStore.Video.Media.SIZE 视频大小
* MediaStore.Video.Media.DATA 视频保存路径
* MediaStore.Video.Media.DATE_MODIFIED 视频文件修改时间
*
*/
String[] projection = new String[]{MediaStore.Video.Media._ID, MediaStore.Video.Media.ALBUM, MediaStore.Video.Media.ARTIST, MediaStore.Video.Media.DATE_ADDED
, MediaStore.Video.Media.DISPLAY_NAME, MediaStore.Video.Media.TITLE, MediaStore.Video.Media.DURATION, MediaStore.Video.Media.SIZE, MediaStore.Video.Media.DATA,
MediaStore.Video.Media.DATE_MODIFIED};
Cursor cursor = getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projection, null, null, MediaStore.Video.Media.DEFAULT_SORT_ORDER);
while (cursor != null && cursor.moveToNext()) {
long id = cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media._ID));
String album = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.ALBUM));
String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.ARTIST));
long addTime = cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media.DATE_ADDED));
String fileName = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DISPLAY_NAME));
String title = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.TITLE));
long duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media.DURATION));
long size=cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media.SIZE));
String data = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DATA));
long modifyTime = cursor.getLong(cursor.getColumnIndex(MediaStore.Video.Media.DATE_MODIFIED));
Video video=new Video();
video.setAddTime(addTime);
video.setAlbum(album);
video.setArtist(artist);
video.setData(data);
video.setDuration(duration);
video.setFileName(fileName);
video.setId(id);
video.setModifyTime(modifyTime);
video.setSize(size);
videos.add(video);
}
adapter.setList(videos);
rv.setAdapter(adapter);
}
}
VideoAdapter
package com.zhqy.mediastoredemo.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.zhqy.mediastoredemo.R;
import java.util.List;
import domain.Picture;
import domain.Video;
/**
* Created by jackal on 2019/10/10.
*/
public class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.AudioViewHolder> {
List<Video> list;
Context context;
public VideoAdapter(Context context){
this.context=context;
}
@Override
public AudioViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View inflate = LayoutInflater.from(context).inflate(R.layout.item_audio, parent, false);
AudioViewHolder viewHolder=new AudioViewHolder(inflate);
viewHolder.tv=inflate.findViewById(R.id.tv);
return viewHolder;
}
@Override
public void onBindViewHolder(AudioViewHolder holder, int position) {
Video video = list.get(position);
holder.tv.setText(video.toString());
}
@Override
public int getItemCount() {
return list.size();
}
class AudioViewHolder extends RecyclerView.ViewHolder{
TextView tv;
public AudioViewHolder(View itemView) {
super(itemView);
}
}
public void setList(List<Video> list) {
this.list = list;
}
}
测试结果
结语
以上就是查询手机媒体库的全部介绍,使用起来也很简单,但各个表的字段较多,在使用时查询自己需要的字段数据。