在 Android MediaStore 中插入音频专辑 [英] Insert audio album in Android MediaStore

查看:36
本文介绍了在 Android MediaStore 中插入音频专辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到有关如何在 MediaStore 中插入专辑的任何信息,我尝试使用

I can't find any information about how to insert an album in MediaStore, I tried using

Uri uri = contentResolver.insert(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, albumValues);

但我收到一个异常,说 Invalid uri.这个 uri 可以很好地检索专辑,但我不能用它来插入专辑.

but I get an exception saying Invalid uri. This uri works fine for retrieving the albums but I can't use it to insert one.

这是其余的代码:

ContentResolver contentResolver = getActivity().getContentResolver();
ContentValues albumValues = new ContentValues();
albumValues.put(Audio.Albums.ALBUM, mAlbumEditText.getText().toString());
albumValues.put(Audio.Albums.ARTIST, mArtistEditText.getText().toString());
int trackNo = 10;
albumValues.put(Audio.Albums.NUMBER_OF_SONGS, trackNo);
Uri uri = contentResolver.insert(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, albumValues);

错误日志:02-24 22:38:19.876: E/AndroidRuntime(5379): java.lang.UnsupportedOperationException: Invalid URI content://media/external/audio/albums

推荐答案

如果文件夹不存在,您需要创建该文件夹,并将其附加到您的 URI.

You'll need to create the folder if it doesn't exist, and append that to your URI.

//Create album folder if it doesn't exist
mImageDir = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), "MyAlbumName");
//Retrieve the path with the folder/filename concatenated
mImageFilePath = new File(mImageDir, "NameOfImage").getAbsolutePath();

//Create new content values
ContentValues values = new ContentValues();
values.put(MediaStore.Images.ImageColumns.DATA, mImageFilePath);
//Add whatever other content values you need
....
mUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

您缺少 ContentValues 的 DATA 部分.这指定了实际的文件路径,并且是必需的.

You're missing the DATA portion of the ContentValues. This specifies the actual file path and is required.

mAudioDir = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_MUSIC), "MyNewAlbumName");
mAudioFilePath = new File(mAudioDir, "myNewAudioFile.mp3").getAbsolutePath();

//This part is what you're missing
albumValues.put(MediaStore.Audio.AudioColumns.DATA, mAudioFilePath);

这篇关于在 Android MediaStore 中插入音频专辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆