如何更新MediaStore.Audio.Albums表中的值

时间:2015-12-03 22:12:44

标签: android

我正在查询MediaStore.Audio.Media以获取歌曲ID,使用ID查找专辑封面URI,然后在URI为空时下载专辑封面,最后使用新图片更新MediaStore.Audio.Albums URI。我无法弄清楚最后一部分。

这里我使用GetCursor()方法

获取光标
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;
}

我在这里使用GetAlbumArtURI()

将专辑封面URI添加到songObject
String[] albumID = {mCursor.getString(5)};
songObject.albumArtURI = GetAlbumArtURI(albumID);

以下是GetAlbumArtURI()方法

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

在这里我测试songObject.albumArtURI是否为空,下载图像,并在元数据中记录URI

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

}

现在,如何将新URI添加回ALBUM_ART列?沿着这些方向的东西?

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

问题在于我不是要更新MediaStore.Audio.Media中的值,而是MediaStore.Audio.Albums

1 个答案:

答案 0 :(得分:0)

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

这可能是最重要的收获

  

许多提供程序允许您通过在URI的末尾附加ID值来访问表中的单个行。例如,要从用户词典中检索_ID为4的行,可以使用此内容URI:

     

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