Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

android - How can I update the album art path using contentResolver?

I want to update/insert a new image for an album in MediaStore but i can't get it work..

This is my code:

public void updateAlbumImage(String path, int albumID) {

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

  int n = contentResolver.update(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, values, MediaStore.Audio.Albums.ALBUM_ID + "=" + albumID, null);
  Log.e(TAG, "updateAlbumImage(" + path + ", " + albumID + "): " + n);
 }

The error is:

03-24 03:09:46.323: ERROR/AndroidRuntime(5319): java.lang.UnsupportedOperationException: Unknown or unsupported URL: content://media/external/audio/albums
03-24 03:09:46.323: ERROR/AndroidRuntime(5319):     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:131)
03-24 03:09:46.323: ERROR/AndroidRuntime(5319):     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:111)
03-24 03:09:46.323: ERROR/AndroidRuntime(5319):     at android.content.ContentProviderProxy.update(ContentProviderNative.java:405)
03-24 03:09:46.323: ERROR/AndroidRuntime(5319):     at android.content.ContentResolver.update(ContentResolver.java:554)
03-24 03:09:46.323: ERROR/AndroidRuntime(5319):     at com.liviu.app.smpp.managers.AudioManager.updateAlbumImage(AudioManager.java:563)
03-24 03:09:46.323: ERROR/AndroidRuntime(5319):     at com.liviu.app.smpp.ShowAlbumsActivity.saveImageFile(ShowAlbumsActivity.java:375)
03-24 03:09:46.323: ERROR/AndroidRuntime(5319):     at com.liviu.app.smpp.ShowAlbumsActivity.onClick(ShowAlbumsActivity.java:350)

Thank you!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

See this post:

Android set Album Thumbnail

The bit you need is here:

    ContentResolver res = context.getContentResolver();
    Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
    if (uri != null) {
        InputStream in = null;
        try {
            in = res.openInputStream(uri);
            return BitmapFactory.decodeStream(in, null, sBitmapOptions);
        } catch (FileNotFoundException ex) {
            // The album art thumbnail does not actually exist. Maybe the user deleted it, or
            // maybe it never existed to begin with.
            Bitmap bm = getArtworkFromFile(context, null, album_id);
            if (bm != null) {
                // Put the newly found artwork in the database.
                // Note that this shouldn't be done for the "unknown" album,
                // but if this method is called correctly, that won't happen.

                // first write it somewhere
                String file = Environment.getExternalStorageDirectory()
                    + "/albumthumbs/" + String.valueOf(System.currentTimeMillis());
                if (ensureFileExists(file)) {
                    try {
                        OutputStream outstream = new FileOutputStream(file);
                        if (bm.getConfig() == null) {
                            bm = bm.copy(Bitmap.Config.RGB_565, false);
                            if (bm == null) {
                                return getDefaultArtwork(context);
                            }
                        }
                        boolean success = bm.compress(Bitmap.CompressFormat.JPEG, 75, outstream);
                        outstream.close();
                        if (success) {
                            ContentValues values = new ContentValues();
                            values.put("album_id", album_id);
                            values.put("_data", file);
                            Uri newuri = res.insert(sArtworkUri, values);
                            if (newuri == null) {
                                // Failed to insert in to the database. The most likely
                                // cause of this is that the item already existed in the
                                // database, and the most likely cause of that is that
                                // the album was scanned before, but the user deleted the
                                // album art from the sd card.
                                // We can ignore that case here, since the media provider
                                // will regenerate the album art for those entries when
                                // it detects this.
                                success = false;
                            }
                        }
                        if (!success) {
                            File f = new File(file);
                            f.delete();
                        }
                    } catch (FileNotFoundException e) {
                        Log.e(TAG, "error creating file", e);
                    } catch (IOException e) {
                        Log.e(TAG, "error creating file", e);
                    }
                }
            } else {
                bm = getDefaultArtwork(context);
            }
            return bm;
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
            }
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...