package how.Album;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
public class albumArt extends ListActivity {
/** Called when the activity is first created. */
private static final BitmapFactory.Options sBitmapOptionsCache = new BitmapFactory.Options();
private static final Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
Context mContext;
ImageView image;
FileManager fM;
ArrayList<Bitmap> albumarts = new ArrayList<Bitmap>();
ArrayList<String> albumInfo = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mContext = this;
fM = new FileManager();
fM.initFiles(this);
image = (ImageView)findViewById(R.id.row_album_art);
String[] cols = new String[] {
MediaStore.Audio.Albums._ID,
MediaStore.Audio.Albums.ARTIST,
MediaStore.Audio.Albums.ALBUM,
MediaStore.Audio.Albums.ALBUM_ART
};
Cursor cur = getContentResolver().query(
MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
cols , null , null, null);
if (cur.moveToFirst()) {
String ablum;
String artist;
String id;
String albumArt;
int idNum;
int artistColumn = cur.getColumnIndex(MediaStore.Audio.Albums.ARTIST);
int ablumColumn = cur.getColumnIndex(MediaStore.Audio.Albums.ALBUM);
int albumArtColumn = cur.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART);
String imagePath;
do {
// Get the field values
idNum = cur.getInt(0); // 레코드의 _id를 가져옵니다.
id = String.valueOf(idNum);
artist = cur.getString(artistColumn); // title 필드의 값을 가져옴
ablum = cur.getString(ablumColumn); // artist 필드의 값을 가져옴
albumArt = cur.getString(albumArtColumn);
Bitmap album_art = getBitmapImage( idNum,200, 200);
albumarts.add(album_art); // 두개 합쳐서 array list 에 넣어줌.
albumInfo.add(ablum + " - " + artist);
} while (cur.moveToNext());
ArrayAdapter<String> songList = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, albumInfo);
setListAdapter(songList);
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Bitmap b = albumarts.get(position );
image.setImageBitmap(b);
}
public Bitmap getBitmapImage(int id, int w, int h) {
ContentResolver res = mContext.getContentResolver();
Uri uri = ContentUris.withAppendedId(sArtworkUri, id);
if (uri != null) {
ParcelFileDescriptor fd = null;
try {
fd = res.openFileDescriptor(uri, "r");
int sampleSize = 1;
sBitmapOptionsCache.inJustDecodeBounds = true;
BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor(), null, sBitmapOptionsCache);
int nextWidth = sBitmapOptionsCache.outWidth >> 1;
int nextHeight = sBitmapOptionsCache.outHeight >> 1;
while (nextWidth>w && nextHeight>h) {
sampleSize <<= 1;
nextWidth >>= 1;
nextHeight >>= 1;
}
sBitmapOptionsCache.inSampleSize = sampleSize;
sBitmapOptionsCache.inJustDecodeBounds = false;
Bitmap b = BitmapFactory.decodeFileDescriptor(
fd.getFileDescriptor(), null, sBitmapOptionsCache);
if (b != null) {
if (sBitmapOptionsCache.outWidth != w || sBitmapOptionsCache.outHeight != h) {
Bitmap tmp = Bitmap.createScaledBitmap(b, w, h, true);
b.recycle();
b = tmp;
}
}
return b;
} catch (FileNotFoundException e) { return null;
} catch (Exception e) { return null;
} finally {
try {
if (fd != null)
fd.close();
} catch (IOException e) {
}
}
}
return null;
}
}
참조:
http://www.androidpub.com/32022
특히 getBitmapImage 는 댓글의 '아름다운 밤'님의 소스와 100% 동일함을 알려드립니다.
댓글 없음:
댓글 쓰기