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);
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.
Here it is the rest of the code:
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);
Error log:
02-24 22:38:19.876: E/AndroidRuntime(5379): java.lang.UnsupportedOperationException: Invalid URI content://media/external/audio/albums
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);
EDIT:
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);