android 获取本地图片、视频
获取所有图片
public static void getPhoto(Context context) { Uri imageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; ContentResolver mContentResolver = context.getContentResolver(); String[] projImage = {MediaStore.Images.Media._ID , MediaStore.Images.Media.DATA , MediaStore.Images.Media.SIZE , MediaStore.Images.Media.DISPLAY_NAME}; // 只查询jpeg和png的图片 //"image/mp4","image/3gp" Cursor mCursor = mContentResolver.query(imageUri, projImage, MediaStore.Images.Media.MIME_TYPE + " in(?, ?, ?)", new String[]{"image/jpeg", "image/png", "image/jpg"}, MediaStore.Images.Media.DATE_MODIFIED + " desc"); int pathIndex = mCursor .getColumnIndex(MediaStore.Images.Media.DATA); if (mCursor.moveToFirst()) { do { // 获取图片的路径 String path = mCursor.getString(pathIndex); // 获取该图片的父路径名 File parentFile = new File(path).getParentFile(); if (parentFile == null) { continue; } //获取的文件地址 String dirPath = parentFile.getAbsolutePath(); } while (mCursor.moveToNext()); } mCursor.close(); }
获取所有视频
public static void getVideo(Context context) { Uri videoUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; ContentResolver mContentResolver = context.getContentResolver(); String[] projVideo = {MediaStore.Video.Thumbnails._ID , MediaStore.Video.Thumbnails.DATA , MediaStore.Video.Media.DURATION , MediaStore.Video.Media.SIZE , MediaStore.Video.Media.DISPLAY_NAME , MediaStore.Video.Media.DATE_MODIFIED}; Cursor mCursor = mContentResolver.query(videoUri, projVideo, MediaStore.Video.Media.MIME_TYPE + " in(?, ?, ?, ?)", new String[]{"video/mp4", "video/3gp", "video/avi", "video/rmvb"}, MediaStore.Video.Media.DATE_MODIFIED + " desc"); if (mCursor != null) { while (mCursor.moveToNext()) { // 获取视频的路径 String path = mCursor.getString(mCursor.getColumnIndex(MediaStore.Video.Media.DATA)); // 获取该视频的父路径名 String dirPath = new File(path).getParentFile().getAbsolutePath(); } mCursor.close(); }