gpt4 book ai didi

android - 从音频文件 Uri 获取专辑封面

转载 作者:太空狗 更新时间:2023-10-29 12:40:40 25 4
gpt4 key购买 nike

我正在尝试从音频文件 Uri 中获取专辑封面,这是我的代码:

// uri is the audio file uri

public static Bitmap getSongCoverArt(Context context, Uri uri){
Bitmap songCoverArt = null;
String[] projections = {MediaStore.Audio.Media.ALBUM_ID};
Cursor cursor = null;
try {
cursor = context.getContentResolver().query(uri, projections, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID);
cursor.moveToFirst();

Uri songCover = Uri.parse("content://media/external/audio/albumart");
Uri uriSongCover = ContentUris.withAppendedId(songCover, column_index);
Log.d(TAG, uriSongCover.toString());
ContentResolver res = context.getContentResolver();
try {
InputStream in = res.openInputStream(uriSongCover);
songCoverArt = BitmapFactory.decodeStream(in);
}catch (FileNotFoundException e){
Log.e(TAG, e.getMessage());
}
}finally {
if(cursor != null){
cursor.close();
}
}

return songCoverArt;
}

此函数始终返回“无内容内容://media/external/audio/albumart/0”

最佳答案

我觉得你的问题出在appendId上

   Uri songCover = Uri.parse("content://media/external/audio/albumart");
Uri uriSongCover = ContentUris.withAppendedId(songCover, column_index)

将 column_index 替换为专辑的 Long _id 而不是 _id 为 0 的列索引。

 album_id = c.getLong(c.getColumnIndex(MediaStore.Audio.Albums._ID));

我的光标在哪里

关于android - 从音频文件 Uri 获取专辑封面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26553349/

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