在Android MediaStore中插入音频专辑

时间:2014-02-25 15:07:22

标签: android uri mediastore

我无法找到有关如何在MediaStore中插入相册的任何信息,我尝试使用

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

但我得到一个例外Invalid uri。这个uri适用于检索专辑,但我不能用它来插入专辑。

这是代码的其余部分:

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

1 个答案:

答案 0 :(得分:1)

如果该文件夹不存在,您将需要创建该文件夹,并将其附加到您的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部分。这指定了实际的文件路径,并且是必需的。

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);