0

I am queryng MediaStore.Audio.Media for a song ID, using the ID to find the album art URI, and then downloading the album art if the URI is null, and finally updating MediaStore.Audio.Albums with the new image URI. I am not able to figure out the last part.

Here I am getting the cursor with GetCursor() method

private Cursor GetCursor() {

    Uri contentURI = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    String[] projection = {MediaStore.Audio.Media.ALBUM,
            MediaStore.Audio.Media.ARTIST,
            MediaStore.Audio.Media.TITLE,
            MediaStore.Audio.Media.DATA,
            MediaStore.Audio.Media.DURATION,
            MediaStore.Audio.Media.ALBUM_ID};
    String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
    String order = MediaStore.Audio.Media.TITLE + " ASC";

    final Cursor mCursor = getContentResolver().query(contentURI, projection, selection, null, order);// Run getContentResolver query
    return mCursor;
}

Here I am adding the album art URI to songObject with GetAlbumArtURI()

String[] albumID = {mCursor.getString(5)};
songObject.albumArtURI = GetAlbumArtURI(albumID);

Here is GetAlbumArtURI() method

private String GetAlbumArtURI(String[] albumID) {

    final Cursor mCursor = getContentResolver().query(
            MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
            new String[]{MediaStore.Audio.Albums.ALBUM_ART},
            MediaStore.Audio.Albums._ID + "=?",
            albumID,
            null
    );

    if (mCursor.moveToFirst()) {

        String var = mCursor.getString(mCursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
        //DatabaseUtils.dumpCursor(mCursor);
        mCursor.close();
        return var;
    }
    else {
        mCursor.close();
        return null;
    }
}

And here I test if the songObject.albumArtURI is null, download the image, and record the URI in the meta data

if (songObject.albumArtURI == null){

    String anotherURI = downloadImageReturnURI();

    ContentValues values = new ContentValues();
    values.put(MediaStore.Audio.Albums.ALBUM_ART, anotherURI);

    // And now I don't know what to do

}

So now, how can I add the new URI back to ALBUM_ART column? Something along these lines?

Uri uri = MediaStore.Audio.Albums.getContentUriForPath(newSoundFile.getAbsolutePath());
Uri newUri = getContentResolver().insert(uri, values);

The problem is that I am not trying to update values in MediaStore.Audio.Media but in MediaStore.Audio.Albums

the_prole
  • 8,275
  • 16
  • 78
  • 163

1 Answers1

-1

From the documentation - Content Provider Basics

// Defines a new Uri object that receives the result of the insertion
Uri mNewUri;

...

// Defines an object to contain the new values to insert
ContentValues mNewValues = new ContentValues();

/*
 * Sets the values of each column and inserts the word. The arguments to the "put"
 * method are "column name" and "value"
 */
mNewValues.put(UserDictionary.Words.APP_ID, "example.user");
mNewValues.put(UserDictionary.Words.LOCALE, "en_US");
mNewValues.put(UserDictionary.Words.WORD, "insert");
mNewValues.put(UserDictionary.Words.FREQUENCY, "100");

mNewUri = getContentResolver().insert(
    UserDictionary.Word.CONTENT_URI,   // the user dictionary content URI
    mNewValues                          // the values to insert
);

And this is probably the most important take away

Many providers allow you to access a single row in a table by appending an ID value to the end of the URI. For example, to retrieve a row whose _ID is 4 from user dictionary, you can use this content URI:

Uri singleUri = ContentUris.withAppendedId(UserDictionary.Words.CONTENT_URI,4);

Community
  • 1
  • 1
the_prole
  • 8,275
  • 16
  • 78
  • 163