3

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

Jelly
  • 4,522
  • 6
  • 26
  • 42
  • Could you post the rest of your ContentValues? It'd help to see if you're missing something important. – Grant Amos Feb 25 '14 at 15:38
  • Is the exception an `UnsupportedOperationException`? Either way, post the log showing the error. – Geobits Feb 25 '14 at 15:48
  • If I change the uri to something that doesn't exist it gives me another exception saying `Unknown uri`, so the uri exists, also i can use `.query(...)` on it. My guess is that albums are inserted in some other way but I can't find any piece of documentation about it. – Jelly Feb 25 '14 at 15:51

1 Answers1

1

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);
Grant Amos
  • 2,256
  • 17
  • 11
  • 1
    Sorry I wasn't explicit enough, I was referring to music albums. – Jelly Feb 25 '14 at 15:26
  • I added what you suggested but it still doesn't work. Adding DATA doesn't make much sense for me because an Album can have multiple songs so multiple paths. The way I understand it is that there is a table with Albums information and each song can refer an entry in that table. I'm able to insert songs in `Media` but I can't insert albums in `Albums` – Jelly Feb 25 '14 at 16:14
  • 1
    You can't add an empty album. When inserting a song, the album should be added automatically based on the mp3 tags. What exactly are you trying to accomplish? Do you want to add empty albums? What is the end-goal? – Grant Amos Feb 25 '14 at 17:49
  • I'm trying to update song tags, ID3 tags are updated but the changes aren't visible in music players since they use MediaStore. I was able to update data in `Audio.Media` which represents an audio file, but I can't insert an album for that song. I'm testing if albums are autoinserted now... – Jelly Feb 25 '14 at 18:11
  • They are not automatically added, they show up in `Audio.Media` but not in `Audio.Albums` – Jelly Feb 25 '14 at 18:20
  • 1
    Well, I'm afraid I'm not sure how to help then. When you use the ContentResolver to add values, they're added to the table. The error you're currently having is that the data you're entering is missing the DATA value which refers to the file's path. Without this, the data can't form a link to the appropriate audio file. In your post here: http://stackoverflow.com/questions/21996228/mediastore-audio-albums-album-id-invalid-column you have access to the DATA column. Retrieve that value, put it into your new values, and then do resolver.insert(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, values) – Grant Amos Feb 25 '14 at 18:44
  • I run some more scenarios and came to the conclusion that if an album added in `Audio.Media` already exists it is left as it is nothing changes, if the album is new it is added somewhere, I don't know where but mediaplayers are able to pick it up. This part of Android is really poor documented. Although your response is not exactly the answer, it pointed me in the right direction, so I'll accept it as the right one. I'll do some more experiments with this. Thanks for your help, you really helped me to find my way out of this – Jelly Feb 25 '14 at 18:53
  • Well thanks :P. If you have a solution to your original question though, you should post it so others that come here can see. That aside, good luck with the rest of your project :P – Grant Amos Feb 25 '14 at 18:59
  • Is there any way I can do this but instead of inserting a new album, would I be able to change the album art by passing the mediastore a specified album id and a image uri? thank you very much. – Jack Aug 14 '15 at 12:08