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

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

概述我想在MediaStore中为相册更新/插入新图像,但我无法让它工作.. 这是我的代码: public void updateAlbumImage(String path, int albumID) { ContentValues values = new ContentValues(); values.put(MediaStore.Audio.Albums.ALBUM_ART, pat 我想在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/albums03-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 – 如何使用contentResolver更新专辑封面路径?全部内容,希望文章能够帮你解决android – 如何使用contentResolver更新专辑封面路径?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1130864.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存