我的目标是能够使用Intent.ACTION_OPEN_DOCUMENT_TREE
获取用户选择的目录,然后从该目录中获取所有音频文件及其元数据。我已经可以使用Intent.ACTION_OPEN_DOCUMENT_TREE
获取用户选择的目录,但无法提取音频文件元数据。我得到的是null而不是元数据。我认为问题在于返回到onActivityResult
的Intent
的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
。这让我相信我需要一种基于onActivityResult
的Intent
返回的Uri从MediaStore中进行选择的方法
这是我第一次尝试使用用户选择的目录获取元数据。
首先,我设置了一个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
列都有一个空值。
在我的第二次尝试中,我尝试使用MediaMetadataRetriever
,但我得到了";java.lang.RuntimeException:setDataSource失败:status=0x80000000":
MediaMetadataRetriever mediaMetadataRetriever;
mediaMetadataRetriever = new MediaMetadataRetriever();
mediaMetadataRetriever.setDataSource(activityMain.getApplicationContext(), childrenUri);
我认为这可能是一个安全问题,因为MediaMetadataRetriever没有具有读取权限的根uri。这是因为从MediaStore Uri检索到的相同文件与MediaMetadataRetriever兼容。
在我的第三次尝试中,我试图根据onActivityResult
的Intent
返回的文档id从MediaStore中进行选择,但我得到了一个";android.database.sqlite.SQLiteException:没有这样的列:document_id(代码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);
}
}
}
如何从用户选择的目录中获取音频文件元数据?
意图。ACTION_OPEN_DOCUMENT_TREE为您提供一个来自不同提供商的SAF uri,而不是媒体存储。
使用saf-uri查询媒体存储是没有意义的。
在最新的安卓系统上,你可以将一些安全存储转换为媒体存储。反之亦然。
然后使用mediastore urie查询mediastore。
我找到了一种解决这个问题的方法:
private void getAudioUri(String displayName) {
ContentResolver contentResolver = activityMain.getContentResolver();
String selection = MediaStore.Audio.Media.DISPLAY_NAME + " == ?";
String[] selectionArgs = new String[]{displayName};
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.mediaStoreUriID = id;
String displayName2 = 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);
}
}
}
}
在这种情况下,传入的displayName来自文档的MediaStore.Audio.Media.DISPLAY_NAME
列。出于某种原因,该元数据可以从从Intent检索的Uri中获得。
我不完全确定这些文件是否有";MediaStore。音频媒体DISPLAY_NAME";总是这是工作在Android派和Marshmelow。