0

album art doesnot set to default image if next song is selected and album art is not present

I am trying to make an media player . If i have the albumk art for previous song and there is no album art for the next song then the next song starts but it displays the old album art only

here is the code i am trying

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

i tried both if else condition as well as the try catch , but even for both ways nothing works

1234567
  • 2,226
  • 4
  • 24
  • 69
  • i don't really know what the problem is. but Picasso is a good way of loading images into views. Maybe with the use of this library the problem is gone – R. Adang Jul 23 '15 at 07:03

1 Answers1

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

this worked may be helpful for someone

1234567
  • 2,226
  • 4
  • 24
  • 69