在日常开发中,我们经常会使用MediaStore来获取手机的音频、图片、视频等相关信息。下面3个是常见的内部类:
MediaStore.Audio 获取音频信息的类
MediaStore.Images 获取图片信息
MediaStore.Video 获取视频信息
query和sql的插叙语句差不多 里面有5个参数
getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, mediaColumns, null, null, null);
第一个参数,uri,rui是什么呢?好吧,上面我们提到了Android提供内容的叫Provider,那么在Android中怎么区分各个Provider?有提供联系人的,有提供图片的等等。所以就需要有一个唯一的标识来标识这个Provider,Uri就是这个标识,android.provider.ContactsContract.Contacts.CONTENT_URI就是提供联系人的内容提供者,可惜这个内容提供者提供的数据很少。
第二个参数,projection,真不知道为什么要用这个单词,这个参数告诉Provider要返回的内容(列Column)
第三个参数,selection,设置条件,相当于SQL语句中的where。null表示不进行筛选。
第四个参数 selectionArgs,这个参数是要配合第三个参数使用的,如果你在第三个参数里面有?,那么你在selectionArgs写的数据就会替换掉?
第五个参数,sortOrder,按照什么进行排序,相当于SQL语句中的Order by
我已经写好几个用来经常查询的方法 直接粘贴使用
/** * 查找视频文件 * * @param context * @return */ public static ArrayListgetVideoList(Context context) { ArrayList sysVideoList = new ArrayList<>(); // MediaStore.Video.Thumbnails.DATA:视频缩略图的文件路径 String[] thumbColumns = {MediaStore.Video.Thumbnails.DATA, MediaStore.Video.Thumbnails.VIDEO_ID}; // 视频其他信息的查询条件 String[] mediaColumns = {MediaStore.Video.Media._ID, MediaStore.Video.Media.SIZE, MediaStore.Video.Media.DATA, MediaStore.Video.Media.TITLE, MediaStore.Video.Media.MIME_TYPE, MediaStore.Video.Media.DURATION, MediaStore.Video.Media.DATE_TAKEN,}; Cursor cursor = context.getContentResolver().query(MediaStore.Video.Media .EXTERNAL_CONTENT_URI, mediaColumns, null, null, null); if (cursor == null) { return sysVideoList; } if (cursor.moveToFirst()) { do { VideoBeam info = new VideoBeam(); int id = cursor.getInt(cursor .getColumnIndex(MediaStore.Video.Media._ID)); Cursor thumbCursor = context.getContentResolver().query( MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI, thumbColumns, MediaStore.Video.Thumbnails.VIDEO_ID + "=" + id, null, null); if (thumbCursor.moveToFirst()) { info.setThumbPath(thumbCursor.getString(thumbCursor .getColumnIndex(MediaStore.Video.Thumbnails.DATA))); } info.setFilePath(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media .DATA))); info.setMimeType(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE))); info.setTitle(cursor.getString(cursor .getColumnIndexOrThrow(MediaStore.Downloads.TITLE))); info.setDuration(cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Downloads.DURATION))); info.setTakeTime(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Downloads.DATE_TAKEN))); info.setVideoSize(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Downloads.SIZE))); sysVideoList.add(info); } while (cursor.moveToNext()); } return sysVideoList; } /** * 查找图片 * * @param context * @return */ public static ArrayList getImgList(Context context) { ArrayList ImagesList = new ArrayList<>(); // MediaStore.Images.Thumbnails.DATA:视频缩略图的文件路径 String[] thumbColumns = {MediaStore.Images.Thumbnails.DATA, MediaStore.Images.Thumbnails._ID}; // 视频其他信息的查询条件 String[] mediaColumns = {MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA, MediaStore.Images.Media.TITLE, MediaStore.Images.Media.MIME_TYPE, MediaStore.Images.Media.DATE_TAKEN, MediaStore.Images.Media.SIZE}; // String[] projection = new String[]{MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DATA, MediaStore.Images.ImageColumns.DISPLAY_NAME}; Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media .EXTERNAL_CONTENT_URI, mediaColumns, null, null, null); if (cursor == null) { return ImagesList; } if (cursor.moveToFirst()) { do { FileImgBean info = new FileImgBean(); int id = cursor.getInt(cursor .getColumnIndex(MediaStore.Images.Media._ID)); Cursor thumbCursor = context.getContentResolver().query( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, thumbColumns, MediaStore.Images.Thumbnails.IMAGE_ID + "=" + id, null, null); if (thumbCursor.moveToFirst()) { info.setThumbPath(thumbCursor.getString(thumbCursor .getColumnIndex(MediaStore.Images.Thumbnails.DATA))); } info.setFilePath(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media .DATA))); info.setMimeType(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.MIME_TYPE))); info.setTitle(cursor.getString(cursor .getColumnIndexOrThrow(MediaStore.Images.Media.TITLE))); info.setTakeTime(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images .Media.DATE_TAKEN))); int columnIndexOrThrow = cursor.getColumnIndex(MediaStore.Images .Media.SIZE); int anInt = cursor.getInt(columnIndexOrThrow); info.setImgSize(anInt + ""); ImagesList.add(info); } while (cursor.moveToNext()); } return ImagesList; } /** * 查找文件 * * @return */ public static ArrayList getAllText(Context context) { ArrayList texts = new ArrayList<>(); String[] projection = new String[]{MediaStore.Files.FileColumns._ID, MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.TITLE, MediaStore.Files.FileColumns.MIME_TYPE, MediaStore.Files.FileColumns.SIZE , MediaStore.Files.FileColumns.DATE_MODIFIED, MediaStore.Files.FileColumns.MIME_TYPE}; //相当于我们常用sql where 后面的写法 String selection = MediaStore.Files.FileColumns.MIME_TYPE + "= ? " + " or " + MediaStore.Files.FileColumns.MIME_TYPE + " = ? " + " or " + MediaStore.Files.FileColumns.MIME_TYPE + " = ? " + " or " + MediaStore.Files.FileColumns.MIME_TYPE + " = ? " + " or " + MediaStore.Files.FileColumns.MIME_TYPE + " = ? "; String[] selectionArgs = new String[]{"application/msword", "text/plain", "application/pdf", "application/vnd.ms-powerpoint", "application/vnd.ms-excel"}; Cursor cursor = context.getContentResolver().query(MediaStore.Files.getContentUri("external"), projection, selection, selectionArgs, MediaStore.Files.FileColumns.DATE_ADDED + " DESC"); FileItemBean fileItem = new FileItemBean(); while (cursor.moveToNext()) { fileItem.setFileId(cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns._ID))); fileItem.setFileName(cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.TITLE))); fileItem.setFilePath(cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA))); fileItem.setFileSize(cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.SIZE))); fileItem.setFileTime(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATE_MODIFIED))); fileItem.setFileType(cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.MIME_TYPE))); texts.add(fileItem); } cursor.close(); return texts; } /** * 查找音频 * * @param context * @return */ public static ArrayList getAudioList(Context context) { ArrayList audioList = new ArrayList<>(); // MediaStore.Images.Thumbnails.DATA:视频缩略图的文件路径 String[] thumbColumns = {MediaStore.Audio.Media.DATA, MediaStore.Audio.Media._ID}; // 视频其他信息的查询条件 String[] mediaColumns = {MediaStore.Audio.Media._ID, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.DURATION, MediaStore.Audio.Media.SIZE}; Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media .EXTERNAL_CONTENT_URI, mediaColumns, null, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER); if (cursor == null) { return audioList; } if (cursor.moveToFirst()) { do { FileAudioBean info = new FileAudioBean(); int id = cursor.getInt(cursor .getColumnIndex(MediaStore.Audio.Media._ID)); Cursor thumbCursor = context.getContentResolver().query( MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, thumbColumns, MediaStore.Audio.Media._ID + "=" + id, null, null); if (thumbCursor.moveToFirst()) { info.setThumbPath(thumbCursor.getString(thumbCursor .getColumnIndex(MediaStore.Audio.Media.DATA))); } info.setAudioTitle(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE))); info.setAudioAlbum(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM))); info.setAudioSinger(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST))); info.setAudioPath(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA))); info.setAudioDuration(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION))); info.setAudioSize(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE))); audioList.add(info); } while (cursor.moveToNext()); } return audioList; } /** * 获取已经安装的应用 * @param context * @return */ public static ArrayList getAppList(Context context) { ArrayList appList = new ArrayList<>(); List packages = context.getPackageManager().getInstalledPackages(0); for (int i = 0; i < packages.size(); i++) { PackageInfo packageInfo = packages.get(i); AppInfo tmpInfo = new AppInfo(); tmpInfo.setAppName(packageInfo.applicationInfo.loadLabel(context.getPackageManager()).toString()); tmpInfo.setPackageName(packageInfo.packageName); tmpInfo.setVersionName(packageInfo.versionName); tmpInfo.setVersionCode(packageInfo.versionCode); tmpInfo.setAppIcon(packageInfo.applicationInfo.loadIcon(context.getPackageManager())); if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { appList.add(tmpInfo);//如果非系统应用,则添加至appList } } return appList; } /** * 压缩包和 zip的共用功法 * @param context * @param keyword * @return */ public static ArrayList searchKeyWord(Context context, String keyword) { ArrayList fileList = new ArrayList<>(); ContentResolver resolver = context.getContentResolver(); Uri uri = MediaStore.Files.getContentUri("external"); Cursor cursor = resolver.query(uri, new String[]{MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.SIZE ,MediaStore.Files.FileColumns.DATE_MODIFIED}, MediaStore.Files.FileColumns.DATA + " LIKE '%" + keyword + "%'", null, MediaStore.Files.FileColumns.DATE_ADDED + " DESC"); if (cursor != null) { while (cursor.moveToNext()) { FileApkBean bean = new FileApkBean(); String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA)); bean.setName(path.substring(path.lastIndexOf("/") + 1)); bean.setSize(cursor.getLong(cursor.getColumnIndexOrThrow( MediaStore.Files.FileColumns.SIZE))+""); bean.setTime(cursor.getString(cursor.getColumnIndexOrThrow( MediaStore.Files.FileColumns.DATE_MODIFIED))); fileList.add(bean); } } cursor.close(); return fileList; }
最后还有个小技巧告诉大家,(也不知道有耐心能翻阅到这么 哈哈哈)
好了 废话不多说 一定到开启 线程 来获取这些文件
因为获取的数据量都挺大的 为了不 出现ANR 影响用户体验 一定要开启线程
直接使用线程池来完成整个操作
// AsyncTask线程池 private class MyTask extends AsyncTask{ @Override protected String doInBackground(String... strings) { //这里来进行 获取数据 看不懂的看这个方法的名字 do in backgroung (are you ok ?) videoList = FindFileHelper.getVideoList(getActivity()); zipList = FindFileHelper.searchKeyWord(getActivity(), ".zip"); return null; } @Override protected void onPostExecute(String s) { //这里来刷新UI set.text()/setadapter()/等等 } }
怎么来使用这个线程池呢?(你猜下)
// 线程变量 MyTask mTask;
mTask = new MyTask(); mTask.execute();
好了以上就是完整的 MediaStore 使用 里面还有各种属性,请猿们自行琢磨,自行理解。