gpt4 book ai didi

android - 如果我知道路径,如何从 MediaStore 获取歌曲 ID?

转载 作者:搜寻专家 更新时间:2023-11-01 07:50:49 27 4
gpt4 key购买 nike

目前我通过歌曲的标题从 MediaStore 获取歌曲 ID:

long id = 0;
ContentResolver cr = context.getContentResolver();

Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.TITLE;
String[] selectionArgs = {songTitle};
String[] projection = {MediaStore.Audio.Media._ID};
String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";

Cursor cursor = cr.query(uri, projection, selection, selectionArgs, sortOrder);

if (cursor != null) {
while (cursor.moveToNext()) {
int idIndex = cursor.getColumnIndex(MediaStore.Audio.Media._ID);
id = Long.parseLong(cursor.getString(idIndex));
}
}
return id;

只有当标题不为空时,这才有效。有没有直接从 mp3 文件的路径直接从 MediaStore 获取歌曲 ID 的方法?

最佳答案

我会回答我自己的问题,因为我找到了解决方案。解决方案是用 DATA 替换 TITLE,因为 DATA 代表 MediaStore 中媒体文件的路径。

public static long getSongIdFromMediaStore(String songPath, Context context) {
long id = 0;
ContentResolver cr = context.getContentResolver();

Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.DATA;
String[] selectionArgs = {songPath};
String[] projection = {MediaStore.Audio.Media._ID};
String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";

Cursor cursor = cr.query(uri, projection, selection + "=?", selectionArgs, sortOrder);


Log.d(Constants.LOG_TAG, songPath);
if (cursor != null) {
while (cursor.moveToNext()) {
int idIndex = cursor.getColumnIndex(MediaStore.Audio.Media._ID);
id = Long.parseLong(cursor.getString(idIndex));
}
}
return id;
}

关于android - 如果我知道路径,如何从 MediaStore 获取歌曲 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35394152/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com