如何从 MediaStore.File 和 MediaStore.File.FileColumn 获取数据
How to get data from MediaStore.File and MediaStore.File.FileColumn
我正在尝试获取其中包含音频文件的所有文件名我已经使用 Mediastore 获取媒体存储 音频、专辑、播放列表和音频数据 但我如何才能获取包含音频文件的文件或文件夹标题。这是我尝试过的代码,但它不正确,因为我无法设置 External_Content_uri。
这是我试过的代码。
private void External() {
try {
String[] proj = {MediaStore.Files.FileColumns.TITLE,
MediaStore.Files.FileColumns._ID,
MediaStore.Files.FileColumns.PARENT,
MediaStore.Files.FileColumns.DATA
};// Can include more data for more details and check it.
String selection =MediaStore.Files.FileColumns.MEDIA_TYPE+"=?";
String[] selectionArgs = {"MediaStore.Files.FileColumns.MEDIA_TYPE_AUDIO"};
String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";
Cursor audioCursor = getContentResolver().query(MediaStore.Files.getContentUri("\"external\""), proj, selection, selectionArgs, sortOrder);
if (audioCursor != null) {
if (audioCursor.moveToFirst()) {
do {
int filetitle = audioCursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.TITLE);
int file_id = audioCursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns._ID);
int fileparent = audioCursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.PARENT);
int filedata = audioCursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA);
Mediafileinfo info = new Mediafileinfo();
info.setData(new File(new File(audioCursor.getString(filedata)).getParent()).getName());
info.setTitle(audioCursor.getString(filetitle));
info.set_id(audioCursor.getString(file_id));
info.setParent(audioCursor.getString(fileparent));
// info.setData(audioCursor.getString(filedata));
audioList.add(info);
} while (audioCursor.moveToNext());
}
}
assert audioCursor != null;
audioCursor.close();
} catch (Exception e) {
e.printStackTrace();
}
}
您好,使用 Android Media Store 和 Android Content Resolver 查询音频、图像和视频等媒体文件,查看本教程
http://www.androiddevelopersolutions.com/2015/12/android-media-store-tutorial-list-all.html
用于列出所有图片:
private void parseAllImages() {
try {
String[] projection = {MediaStore.Images.Media.DATA};
cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection, // Which columns to return
null, // Return all rows
null,
null);
int size = cursor.getCount();
/******* If size is 0, there are no images on the SD Card. *****/
if (size == 0) {
} else {
int thumbID = 0;
while (cursor.moveToNext()) {
int file_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
/**************** Captured image details ************/
/***** Used to show image on view in LoadImagesFromSDCard class ******/
String path = cursor.getString(file_ColumnIndex);
String fileName = path.substring(path.lastIndexOf("/") + 1, path.length());
MediaFileInfo mediaFileInfo = new MediaFileInfo();
mediaFileInfo.setFilePath(path);
mediaFileInfo.setFileName(fileName);
mediaFileInfo.setFileType(type);
mediaList.add(mediaFileInfo);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
用于列出所有视频(.mp4)
private void parseAllVideo() {
try {
String name = null;
String[] thumbColumns = {MediaStore.Video.Thumbnails.DATA,
MediaStore.Video.Thumbnails.VIDEO_ID};
int video_column_index;
String[] proj = {MediaStore.Video.Media._ID,
MediaStore.Video.Media.DATA,
MediaStore.Video.Media.DISPLAY_NAME,
MediaStore.Video.Media.SIZE};
Cursor videocursor = getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
proj, null, null, null);
int count = videocursor.getCount();
Log.d("No of video", "" + count);
for (int i = 0; i < count; i++) {
MediaFileInfo mediaFileInfo = new MediaFileInfo();
video_column_index = videocursor
.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME);
videocursor.moveToPosition(i);
name = videocursor.getString(video_column_index);
mediaFileInfo.setFileName(name);
int column_index = videocursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
videocursor.moveToPosition(i);
String filepath = videocursor.getString(column_index);
mediaFileInfo.setFilePath(filepath);
mediaFileInfo.setFileType(type);
mediaList.add(mediaFileInfo);
// id += " Size(KB):" +
// videocursor.getString(video_column_index);
}
videocursor.close();
} catch (Exception e) {
e.printStackTrace();
}
}
列出所有音频
private void parseAllAudio() {
try {
String TAG = "Audio";
Cursor cur = getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
null);
if (cur == null) {
// Query failed...
Log.e(TAG, "Failed to retrieve music: cursor is null :-(");
}
else if (!cur.moveToFirst()) {
// Nothing to query. There is no music on the device. How boring.
Log.e(TAG, "Failed to move cursor to first row (no query results).");
}else {
Log.i(TAG, "Listing...");
// retrieve the indices of the columns where the ID, title, etc. of the song are
// add each song to mItems
do {
int artistColumn = cur.getColumnIndex(MediaStore.Audio.Media.ARTIST);
int titleColumn = cur.getColumnIndex(MediaStore.Audio.Media.TITLE);
int albumColumn = cur.getColumnIndex(MediaStore.Audio.Media.ALBUM);
int durationColumn = cur.getColumnIndex(MediaStore.Audio.Media.DURATION);
int idColumn = cur.getColumnIndex(MediaStore.Audio.Media._ID);
int filePathIndex = cur.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
Log.i(TAG, "Title column index: " + String.valueOf(titleColumn));
Log.i(TAG, "ID column index: " + String.valueOf(titleColumn));
Log.i("Final ", "ID: " + cur.getString(idColumn) + " Title: " + cur.getString(titleColumn) + "Path: " + cur.getString(filePathIndex));
MediaFileInfo audio = new MediaFileInfo();
audio.setFileName(cur.getString(titleColumn));
audio.setFilePath(cur.getString(filePathIndex));
audio.setFileType(type);
mediaList.add(audio);
} while (cur.moveToNext());
}
} catch (Exception e) {
e.printStackTrace();
}
}
写一个新的答案,因为你只需要音频文件的文件夹名称。
所以这里最好使用MediaStore.Audio.Media
而不是MediaStore.File
。使用下面的音频文件路径获取文件夹名称。
new File(new File(audioCursor.getString(filedata)).getParent()).getName()
private void External() {
try {
Uri externalUri= MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
projection=new String[]{MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DATA,;
String selection =null;
String[] selectionArgs = null;
String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";
Cursor audioCursor = getContentResolver().query(externalUri, proj, selection, selectionArgs, sortOrder);
if (audioCursor != null) {
if (audioCursor.moveToFirst()) {
do {
int filetitle = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
int file_id = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID);
int filePath = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
Mediafileinfo info = new Mediafileinfo();
info.setData(new File(new File(audioCursor.getString(filedata)).getParent()).getName());
info.setTitle(audioCursor.getString(filetitle));
info.set_id(audioCursor.getString(file_id));
audioList.add(info);
} while (audioCursor.moveToNext());
}
}
assert audioCursor != null;
audioCursor.close();
} catch (Exception e) {
e.printStackTrace();
}
}
我正在尝试获取其中包含音频文件的所有文件名我已经使用 Mediastore 获取媒体存储 音频、专辑、播放列表和音频数据 但我如何才能获取包含音频文件的文件或文件夹标题。这是我尝试过的代码,但它不正确,因为我无法设置 External_Content_uri。 这是我试过的代码。
private void External() {
try {
String[] proj = {MediaStore.Files.FileColumns.TITLE,
MediaStore.Files.FileColumns._ID,
MediaStore.Files.FileColumns.PARENT,
MediaStore.Files.FileColumns.DATA
};// Can include more data for more details and check it.
String selection =MediaStore.Files.FileColumns.MEDIA_TYPE+"=?";
String[] selectionArgs = {"MediaStore.Files.FileColumns.MEDIA_TYPE_AUDIO"};
String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";
Cursor audioCursor = getContentResolver().query(MediaStore.Files.getContentUri("\"external\""), proj, selection, selectionArgs, sortOrder);
if (audioCursor != null) {
if (audioCursor.moveToFirst()) {
do {
int filetitle = audioCursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.TITLE);
int file_id = audioCursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns._ID);
int fileparent = audioCursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.PARENT);
int filedata = audioCursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA);
Mediafileinfo info = new Mediafileinfo();
info.setData(new File(new File(audioCursor.getString(filedata)).getParent()).getName());
info.setTitle(audioCursor.getString(filetitle));
info.set_id(audioCursor.getString(file_id));
info.setParent(audioCursor.getString(fileparent));
// info.setData(audioCursor.getString(filedata));
audioList.add(info);
} while (audioCursor.moveToNext());
}
}
assert audioCursor != null;
audioCursor.close();
} catch (Exception e) {
e.printStackTrace();
}
}
您好,使用 Android Media Store 和 Android Content Resolver 查询音频、图像和视频等媒体文件,查看本教程
http://www.androiddevelopersolutions.com/2015/12/android-media-store-tutorial-list-all.html
用于列出所有图片:
private void parseAllImages() {
try {
String[] projection = {MediaStore.Images.Media.DATA};
cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection, // Which columns to return
null, // Return all rows
null,
null);
int size = cursor.getCount();
/******* If size is 0, there are no images on the SD Card. *****/
if (size == 0) {
} else {
int thumbID = 0;
while (cursor.moveToNext()) {
int file_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
/**************** Captured image details ************/
/***** Used to show image on view in LoadImagesFromSDCard class ******/
String path = cursor.getString(file_ColumnIndex);
String fileName = path.substring(path.lastIndexOf("/") + 1, path.length());
MediaFileInfo mediaFileInfo = new MediaFileInfo();
mediaFileInfo.setFilePath(path);
mediaFileInfo.setFileName(fileName);
mediaFileInfo.setFileType(type);
mediaList.add(mediaFileInfo);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
用于列出所有视频(.mp4)
private void parseAllVideo() {
try {
String name = null;
String[] thumbColumns = {MediaStore.Video.Thumbnails.DATA,
MediaStore.Video.Thumbnails.VIDEO_ID};
int video_column_index;
String[] proj = {MediaStore.Video.Media._ID,
MediaStore.Video.Media.DATA,
MediaStore.Video.Media.DISPLAY_NAME,
MediaStore.Video.Media.SIZE};
Cursor videocursor = getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
proj, null, null, null);
int count = videocursor.getCount();
Log.d("No of video", "" + count);
for (int i = 0; i < count; i++) {
MediaFileInfo mediaFileInfo = new MediaFileInfo();
video_column_index = videocursor
.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME);
videocursor.moveToPosition(i);
name = videocursor.getString(video_column_index);
mediaFileInfo.setFileName(name);
int column_index = videocursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
videocursor.moveToPosition(i);
String filepath = videocursor.getString(column_index);
mediaFileInfo.setFilePath(filepath);
mediaFileInfo.setFileType(type);
mediaList.add(mediaFileInfo);
// id += " Size(KB):" +
// videocursor.getString(video_column_index);
}
videocursor.close();
} catch (Exception e) {
e.printStackTrace();
}
}
列出所有音频
private void parseAllAudio() {
try {
String TAG = "Audio";
Cursor cur = getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
null);
if (cur == null) {
// Query failed...
Log.e(TAG, "Failed to retrieve music: cursor is null :-(");
}
else if (!cur.moveToFirst()) {
// Nothing to query. There is no music on the device. How boring.
Log.e(TAG, "Failed to move cursor to first row (no query results).");
}else {
Log.i(TAG, "Listing...");
// retrieve the indices of the columns where the ID, title, etc. of the song are
// add each song to mItems
do {
int artistColumn = cur.getColumnIndex(MediaStore.Audio.Media.ARTIST);
int titleColumn = cur.getColumnIndex(MediaStore.Audio.Media.TITLE);
int albumColumn = cur.getColumnIndex(MediaStore.Audio.Media.ALBUM);
int durationColumn = cur.getColumnIndex(MediaStore.Audio.Media.DURATION);
int idColumn = cur.getColumnIndex(MediaStore.Audio.Media._ID);
int filePathIndex = cur.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
Log.i(TAG, "Title column index: " + String.valueOf(titleColumn));
Log.i(TAG, "ID column index: " + String.valueOf(titleColumn));
Log.i("Final ", "ID: " + cur.getString(idColumn) + " Title: " + cur.getString(titleColumn) + "Path: " + cur.getString(filePathIndex));
MediaFileInfo audio = new MediaFileInfo();
audio.setFileName(cur.getString(titleColumn));
audio.setFilePath(cur.getString(filePathIndex));
audio.setFileType(type);
mediaList.add(audio);
} while (cur.moveToNext());
}
} catch (Exception e) {
e.printStackTrace();
}
}
写一个新的答案,因为你只需要音频文件的文件夹名称。
所以这里最好使用MediaStore.Audio.Media
而不是MediaStore.File
。使用下面的音频文件路径获取文件夹名称。
new File(new File(audioCursor.getString(filedata)).getParent()).getName()
private void External() {
try {
Uri externalUri= MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
projection=new String[]{MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DATA,;
String selection =null;
String[] selectionArgs = null;
String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";
Cursor audioCursor = getContentResolver().query(externalUri, proj, selection, selectionArgs, sortOrder);
if (audioCursor != null) {
if (audioCursor.moveToFirst()) {
do {
int filetitle = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
int file_id = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID);
int filePath = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
Mediafileinfo info = new Mediafileinfo();
info.setData(new File(new File(audioCursor.getString(filedata)).getParent()).getName());
info.setTitle(audioCursor.getString(filetitle));
info.set_id(audioCursor.getString(file_id));
audioList.add(info);
} while (audioCursor.moveToNext());
}
}
assert audioCursor != null;
audioCursor.close();
} catch (Exception e) {
e.printStackTrace();
}
}