如果选择了下一首歌曲并且不存在专辑封面,则专辑封面不会设置为默认图像

时间:2015-07-23 06:54:18

标签: android bitmap albumart

如果选择了下一首歌并且没有专辑封面,

专辑封面不会设置为默认图像

我正在尝试制作媒体播放器。如果我有上一首歌的专辑封面,下一首歌没有专辑封面,那么下一首歌就会开始,但它只显示旧专辑封面

这是我正在尝试的代码

long albumId = cursor
                .getLong(cursor
                        .getColumnIndexOrThrow(android.provider.MediaStore.Audio.Media.ALBUM_ID));
        final Uri ART_CONTENT_URI = Uri
                .parse("content://media/external/audio/albumart");
        Uri albumArtUri = ContentUris.withAppendedId(ART_CONTENT_URI, albumId);
        Bitmap actuallyUsableBitmap = null;
        BitmapFactory.Options options = new BitmapFactory.Options();
        // options.inSampleSize = 1;
        AssetFileDescriptor fileDescriptor = null;
        try {
            fileDescriptor = this.getContentResolver().openAssetFileDescriptor(
                    albumArtUri, "r");
            actuallyUsableBitmap = BitmapFactory.decodeFileDescriptor(
                    fileDescriptor.getFileDescriptor(), null, options);
            fileDescriptor = null;
            if (actuallyUsableBitmap != null) {
                album_art.setImageBitmap(actuallyUsableBitmap);
            } else if (actuallyUsableBitmap == null) {
                album_art.setBackgroundResource(R.drawable.ic_launcher);
            }
        }

        catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block

            //album_art.setBackgroundResource(R.drawable.ic_launcher);
            //Toast.makeText(this, e1 + "geting Id", Toast.LENGTH_SHORT).show();

            e1.printStackTrace();
        }

我同时尝试了if else条件以及try catch,但即使两种方式都没有效果

1 个答案:

答案 0 :(得分:1)

long albumId = cursor
                .getLong(cursor
                        .getColumnIndexOrThrow(android.provider.MediaStore.Audio.Media.ALBUM_ID));]

try {

            Bitmap albumArt = getAlbumart(albumId);
            if (albumArt != null) {
                album_art.setBackgroundDrawable(new BitmapDrawable(albumArt));
            } else {
                album_art.setBackgroundDrawable(new BitmapDrawable(
                        getDefaultAlbumArt()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public Bitmap getAlbumart(Long album_id) {
        Bitmap bm = null;
        try {
            final Uri sArtworkUri = Uri
                    .parse("content://media/external/audio/albumart");

            Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);

            ParcelFileDescriptor pfd = this.getContentResolver()
                    .openFileDescriptor(uri, "r");

            if (pfd != null) {
                FileDescriptor fd = pfd.getFileDescriptor();
                bm = BitmapFactory.decodeFileDescriptor(fd);
            }
        } catch (Exception e) {
        }
        return bm;
    }

    public Bitmap getDefaultAlbumArt() {
        Bitmap bm = null;
        BitmapFactory.Options options = new BitmapFactory.Options();
        try {
            bm = BitmapFactory.decodeResource(getResources(),
                    R.drawable.ic_launcher, options);
        } catch (Error ee) {
        } catch (Exception e) {
        }
        return bm;
    }

这可能对某人有帮助