get Song Album Art - Android Media

Android examples for Media:Song

Description

get Song Album Art

Demo Code


//package com.java2s;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import android.util.Log;

public class Main {
    public static String getAlbumArt(String trackId, Context context) {
        Log.e("trackId", "trackId---" + trackId);
        Log.e("context_", "context:" + context + "");
        String mUriTrack = "content://media/external/audio/media/#";
        String[] projection = new String[] { "album_id" };
        String selection = "_id = ?";
        String[] selectionArgs = new String[] { trackId };
        Cursor cur = context.getContentResolver().query(
                Uri.parse(mUriTrack), projection, selection, selectionArgs,
                null);// w  w w .  java2  s  .  c om
        int album_id = 0;
        if (cur.getCount() > 0 && cur.getColumnCount() > 0) {
            cur.moveToNext();
            album_id = cur.getInt(0);
        }
        cur.close();
        cur = null;

        if (album_id < 0) {
            return null;
        }
        String mUriAlbums = "content://media/external/audio/albums";
        projection = new String[] { "album_art" };
        cur = context.getContentResolver().query(
                Uri.parse(mUriAlbums + "/" + Integer.toString(album_id)),
                projection, null, null, null);

        String album_art = null;
        if (cur.getCount() > 0 && cur.getColumnCount() > 0) {
            cur.moveToNext();
            album_art = cur.getString(0);
        }
        cur.close();
        cur = null;
        Log.e("album_art", "album_art---" + album_art);
        Log.e("trackId", "trackId---" + trackId);
        return album_art;
    }
}

Related Tutorials