我有一个imageview mAlbumArtLarge,它显示了歌曲的专辑封面,但是如何检查专辑封面是否不存在以及它是否不存在显示我的列表中的一个图像。
我的代码:
private void loadAlbumArt(){
Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri, songList.get(songIndex).getAlbumID());
int[] myImageList = new int[]{R.drawable.image1, R.drawable.image2, R.drawable.image3,
R.drawable.image4, R.drawable.image5, R.drawable.image6, R.drawable.image7 };
Random random = new Random();
int randomNumber = random.nextInt(myImageList.length);
try {
InputStream inputStream = getContentResolver().openInputStream(albumArtUri);
//Get the bitmap from the ImageView.
Bitmap image = BitmapFactory.decodeStream(inputStream);
//Let's apply Gaussian blur effect with radius "10.5" and set to ImageView.
mAlbumArtLarge.setImageBitmap(new BlurUtil().blur(MainActivity.this, image, 10.5f));
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(this, "Unable to find album art", Toast.LENGTH_LONG).show();
}
}
显示模糊的专辑封面,但如果没有专辑封面,我想显示myImageList中的随机图像。
我怎样才能做到这一点?
谢谢,
尝试先获取歌曲的ID。根据你的代码我无法计算你的数据songList和它的结构索引。但如果它返回一个int使用它
int id = songList.get(songIndex).getAlbumID();
if (id != null && id > 0)
{
Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri, songList.get(songIndex).getAlbumID());
}