android – 如何使用contentResolver更新专辑封面路径?

我想在MediaStore中为相册更新/插入新图像,但我无法让它工作..

这是我的代码

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

错误是:

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)

谢谢!

解决方法

看这篇文章

Android set Album Thumbnail

你需要的是这里:

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,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,e);
                    }
                }
            } else {
                bm = getDefaultArtwork(context);
            }
            return bm;
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
            }
        }
    }

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...