public static void setAlbumArt(ImageView imageview, String file, boolean compress) { String albumArtpath = allSongsList.getAlbumArt(file); if (albumArtpath != null) { File albumArtFile = new File(albumArtpath); Bitmap bm = null; InputStream iStream1 = null; InputStream iStream2 = null; try { iStream1 = new BufferedInputStream(new FileInputStream(albumArtFile)); if (!compress) { bm = BitmapFactory.decodeStream(iStream1); } else { iStream2 = new BufferedInputStream(new FileInputStream(albumArtFile)); bm = decodeFile2(iStream1, iStream2, 100, 100); } imageview.setImageBitmap(bm); } catch (FileNotFoundException e) { MediaMetadataRetriever md = new MediaMetadataRetriever(); md.setDataSource(file); byte[] art = md.getEmbeddedPicture(); if (art != null) { iStream1 = new ByteArrayInputStream(md.getEmbeddedPicture()); if (!compress) { bm = BitmapFactory.decodeStream(iStream1); } else { iStream2 = new ByteArrayInputStream(md.getEmbeddedPicture()); bm = decodeFile2(iStream1, iStream2, 100, 100); } imageview.setImageBitmap(bm); } else { imageview.setImageDrawable( imageview .getContext() .getResources() .getDrawable(R.drawable.ic_expandplayer_placeholder)); } } } else { imageview.setImageDrawable( imageview .getContext() .getResources() .getDrawable(R.drawable.ic_expandplayer_placeholder)); } }
/** * 获取本地音乐缩略图的方法 * * @param filePath * @return */ @SuppressLint("NewApi") public static Bitmap getLocalMusicBitmap(String filePath) { Bitmap bitmap = null; MediaMetadataRetriever mmr = new MediaMetadataRetriever(); mmr.setDataSource(filePath); bitmap = Bytes2Bimap(mmr.getEmbeddedPicture()); return bitmap; }
// two extra methods for async-loading of images public static Bitmap decodeAlbumArt(String file, boolean compress) { Bitmap toReturn = null; String albumArtpath = allSongsList.getAlbumArt(file); if (albumArtpath != null) { File albumArtFile = new File(albumArtpath); Bitmap bm = null; InputStream iStream1 = null; InputStream iStream2 = null; try { iStream1 = new BufferedInputStream(new FileInputStream(albumArtFile)); if (!compress) { bm = BitmapFactory.decodeStream(iStream1); } else { iStream2 = new BufferedInputStream(new FileInputStream(albumArtFile)); bm = decodeFile2(iStream1, iStream2, 100, 100); } } catch (FileNotFoundException e) { MediaMetadataRetriever md = new MediaMetadataRetriever(); md.setDataSource(file); byte[] art = md.getEmbeddedPicture(); if (art != null) { iStream1 = new ByteArrayInputStream(md.getEmbeddedPicture()); if (!compress) { bm = BitmapFactory.decodeStream(iStream1); } else { iStream2 = new ByteArrayInputStream(md.getEmbeddedPicture()); bm = decodeFile2(iStream1, iStream2, 100, 100); } } } toReturn = bm; } return toReturn; }
private void addSongToList(File file) { if (file.getName().endsWith(".mp3")) { DatabaseHandler db = new DatabaseHandler(Setting.this); String songName = file.getName().substring(0, (file.getName().length() - 4)); String songPath = file.getPath(); MediaMetadataRetriever media = new MediaMetadataRetriever(); media.setDataSource(songPath); byte[] data = media.getEmbeddedPicture(); String songArtist = media.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST); String songAlbum = media.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM); media.release(); db.addSongData(new SongData(songName, songPath, songArtist, songAlbum, data, 0)); db.close(); } }
@Override public Bitmap getBitmap(int position) { MusicInfo musicInfo = (MusicInfo) getItem(position); MediaMetadataRetriever retriever = new MediaMetadataRetriever(); retriever.setDataSource(mContext, musicInfo.getUri()); byte[] albumArt = retriever.getEmbeddedPicture(); BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 4; // 2의 배수 Bitmap bitmap; if (null != albumArt) { bitmap = BitmapFactory.decodeByteArray(albumArt, 0, albumArt.length, options); } else { bitmap = BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_no_image); } // id 로부터 bitmap 생성 return bitmap; }