我的目标是能够使用用户选择的目录Intent.ACTION_OPEN_DOCUMENT_TREE
,然后从该目录中获取所有音频文件及其元数据。我已经可以使用 获取用户选择的目录Intent.ACTION_OPEN_DOCUMENT_TREE
,但我无法提取音频文件元数据。我得到 null 而不是元数据。我认为问题在于返回到onActivityResult
'sIntent
的 Uri 与 MediaStore Uri 不兼容。
我目前可以使用以下代码从 MediaStore 获取元数据:
void getAudioFiles(){
String[] projection = new String[]{
MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.IS_MUSIC,
MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.TITLE, MediaStore.Images.Media.DATA
};
String selection = MediaStore.Audio.Media.IS_MUSIC + " != ?";
String[] selectionArgs = new String[]{"0"};
String sortOrder = MediaStore.Video.Media.TITLE + " ASC";
try (Cursor cursor = getApplicationContext().getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, selection, selectionArgs, sortOrder)) {
if (cursor != null) {
List<Uri> newURIs = getURIs(cursor);
}
}
}
private List<Uri> getURIs(Cursor cursor) {
ArrayList<Uri> newURIs = new ArrayList<>();
int idCol = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID);
int nameCol = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
int titleCol = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
int artistCol = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST);
int dataCol = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
while (cursor.moveToNext()) {
long id = cursor.getLong(idCol);
String displayName = cursor.getString(nameCol);
String title = cursor.getString(titleCol);
String artist = cursor.getString(artistCol);
String data = cursor.getString(dataCol);
Uri uri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id);
if (!uriMap.containsKey(uri)) {
AudioURI audioURI = new AudioURI(uri, data, displayName, artist, title, id);
uriMap.put(uri, audioURI);
}
newURIs.add(uri);
}
return newURIs;
}
在这里我查询MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
. 这让我相信我需要一种方法来根据返回的 Uri 从 MediaStore 中onActivityResult
选择Intent
这是我第一次尝试使用用户选择的目录获取元数据。
第一次我设置了一个 onClickListener 来要求用户选择一个目录:
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
String title = getResources().getString(R.string.pick_folder);
Intent chooser = Intent.createChooser(intent, title);
if (intent.resolveActivity(activityMain.getPackageManager()) != null) {
startActivityForResult(chooser, REQUEST_CODE_OPEN_FOLDER);
}
}
然后我得到结果并尝试解析数据:
@Override
public void onActivityResult(int requestCode, int resultCode,
Intent resultData) {
if (requestCode == REQUEST_CODE_OPEN_FOLDER && resultCode == Activity.RESULT_OK) {
Uri uri = null;
if (resultData != null) {
uri = resultData.getData();
final int takeFlags = resultData.getFlags()
& (Intent.FLAG_GRANT_READ_URI_PERMISSION);
ContentResolver contentResolver = activityMain.getContentResolver();
contentResolver.takePersistableUriPermission(uri, takeFlags);
traverseDirectoryEntries(uri);
}
}
}
void traverseDirectoryEntries(Uri rootUri) {
Uri childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(
rootUri, DocumentsContract.getTreeDocumentId(rootUri));
getFiles(childrenUri, rootUri);
}
private void getFiles(Uri childrenUri, Uri rootUri) {
ContentResolver contentResolver = activityMain.getContentResolver();
List<TreeViewNode> treeViewNodes = new LinkedList<>();
String selection = MediaStore.Audio.Media.IS_MUSIC + " != ? OR" +
DocumentsContract.Document.COLUMN_MIME_TYPE + " == ?";
String[] selectionArgs = new String[]{"0"}, DocumentsContract.Document.MIME_TYPE_DIR};
try (Cursor cursor = contentResolver.query(childrenUri, new String[]{
DocumentsContract.Document.COLUMN_DOCUMENT_ID,
DocumentsContract.Document.COLUMN_DISPLAY_NAME,
DocumentsContract.Document.COLUMN_MIME_TYPE,
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.DATA},
selection, selectionArgs, null)) {
if (cursor != null) {
int idCol = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID);
int nameCol = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
int titleCol = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
int artistCol = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST);
int dataCol = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
while (cursor.moveToNext()) {
String docId = cursor.getString(0);
String name = cursor.getString(1);
String mime = cursor.getString(2);
long id = cursor.getLong(idCol);
this.uriID = id;
String displayName = cursor.getString(nameCol);
String title = cursor.getString(titleCol);
String artist = cursor.getString(artistCol);
String data = cursor.getString(dataCol);
Uri uri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id);
if (isDirectory(mime)) {
Uri newNode = DocumentsContract.buildChildDocumentsUriUsingTree(
rootUri, docId);
getFiles(newNode, rootUri);
} else {
audioFiles.add(childrenUri);
}
}
}
}
}
private static boolean isDirectory(String mimeType) {
return DocumentsContract.Document.MIME_TYPE_DIR.equals(mimeType);
}
这适用于查找文件,但不能获取元数据。每MediaStore.Audio.Media
列都有一个空值。
对于我的第二次尝试,我尝试使用 a MediaMetadataRetriever
,但我得到“java.lang.RuntimeException: setDataSource failed: status = 0x80000000”:
MediaMetadataRetriever mediaMetadataRetriever;
mediaMetadataRetriever = new MediaMetadataRetriever();
mediaMetadataRetriever.setDataSource(activityMain.getApplicationContext(), childrenUri);
我认为这可能是一个安全问题,因为 MediaMetadataRetriever 没有具有读取权限的根 uri。这是因为从 MediaStore Uri 检索的相同文件与 MediaMetadataRetriever 兼容。
对于我的第三次尝试,我尝试根据onActivityResult
's返回的文档 ID 从 MediaStore 中进行选择Intent
,但我得到一个“android.database.sqlite.SQLiteException: no such column: document_id (code 1 SQLITE_ERROR)”:
ContentResolver contentResolver = activityMain.getContentResolver();
String selection = DocumentsContract.Document.COLUMN_DOCUMENT_ID + " == ?";
String[] selectionArgs = new String[]{docID};
try (Cursor cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[]{
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.DATA
},
selection, selectionArgs, null)) {
if (cursor != null) {
while (cursor.moveToNext()) {
int idCol = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID);
int nameCol = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
int titleCol = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
int artistCol = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST);
int dataCol = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
long id = cursor.getLong(idCol);
this.uriID = id;
String displayName = cursor.getString(nameCol);
String title = cursor.getString(titleCol);
String artist = cursor.getString(artistCol);
String data = cursor.getString(dataCol);
Uri uri1 = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id);
}
}
}
应该如何从用户选择的目录中获取音频文件元数据?