0

Album art does not show in custom simplecursoradpter

i have succeeded showing all the albums names in the listview but unable to display album art etc

while every thing works fine for the text and animation the album art just doesnot appear in the list view

here is the code i am trying

public class othercustom extends ListActivity {

    //define source of MediaStore.Images.Media, internal or external storage
    //final Uri sourceUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    final Uri sourceUri = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
    //final Uri thumbUri = MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI;
    final String thumbUri = android.provider.MediaStore.Audio.Albums.ALBUM_ART;
    final String thumb_DATA = android.provider.MediaStore.Audio.Albums.ALBUM;
    final String thumb_IMAGE_ID = android.provider.MediaStore.Audio.Albums._ID;

    //SimpleCursorAdapter mySimpleCursorAdapter;
    MyAdapter mySimpleCursorAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);

        String[] from = {android.provider.MediaStore.Audio.Albums.ALBUM};
        int[] to = {android.R.id.text1};

        CursorLoader cursorLoader = new CursorLoader(
                this, 
                sourceUri, 
                null, 
                null, 
                null, 
                android.provider.MediaStore.Audio.Albums.ALBUM);

        Cursor cursor = cursorLoader.loadInBackground();

        mySimpleCursorAdapter = new MyAdapter(
                this, 
                android.R.layout.simple_list_item_1, 
                cursor, 
                from, 
                to
                );

        setListAdapter(mySimpleCursorAdapter);

        getListView().setOnItemClickListener(myOnItemClickListener);
    }

    OnItemClickListener myOnItemClickListener
    = new OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            Cursor cursor = mySimpleCursorAdapter.getCursor();
            cursor.moveToPosition(position);

            int int_ID = cursor.getInt(cursor.getColumnIndex(android.provider.MediaStore.Audio.Albums._ID));
            getThumbnail(int_ID);
        }};

    private Bitmap getThumbnail(int id){

        String[] thumbColumns = {thumb_DATA, thumb_IMAGE_ID};

        CursorLoader thumbCursorLoader = new CursorLoader(
                this, 
                sourceUri, 
                null, 
                null, 
                null, 
                null);

        Cursor thumbCursor = thumbCursorLoader.loadInBackground();

        Bitmap thumbBitmap = null;
        if(thumbCursor.moveToFirst()){
            //int thCulumnIndex = thumbCursor.getColumnIndex(thumb_DATA);

            //String thumbPath = thumbCursor.getString(thCulumnIndex);
            String thumbPath = android.provider.MediaStore.Audio.Albums.ALBUM_ART;  
            Toast.makeText(getApplicationContext(), 
                    thumbPath, 
                    Toast.LENGTH_LONG).show();

            thumbBitmap = BitmapFactory.decodeFile(thumbPath);

            //Create a Dialog to display the thumbnail
            AlertDialog.Builder thumbDialog = new AlertDialog.Builder(othercustom.this);
            ImageView thumbView = new ImageView(othercustom.this);
            thumbView.setImageBitmap(thumbBitmap);
            LinearLayout layout = new LinearLayout(othercustom.this);
            layout.setOrientation(LinearLayout.VERTICAL);
            layout.addView(thumbView);
            thumbDialog.setView(layout);
            thumbDialog.show();

        }else{
            Toast.makeText(getApplicationContext(), 
                    "NO Thumbnail!", 
                    Toast.LENGTH_LONG).show();
        }

        return thumbBitmap;
    }

    public class MyAdapter extends SimpleCursorAdapter{

        Cursor myCursor;
        Context myContext;

        public MyAdapter(Context context, int layout, Cursor c, String[] from,
                int[] to) {
            super(context, layout, c, from, to);

            myCursor = c;
            myContext = context;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            if(row==null){
                LayoutInflater inflater=getLayoutInflater();
                row=inflater.inflate(R.layout.itemtexview, parent, false);  
            }

            ImageView thumbV = (ImageView)row.findViewById(R.id.thumb);
            TextView textV = (TextView)row.findViewById(R.id.text);

            myCursor.moveToPosition(position);

            int myID = myCursor.getInt(myCursor.getColumnIndex(android.provider.MediaStore.Audio.Albums._ID));
            String myData = myCursor.getString(myCursor.getColumnIndex(android.provider.MediaStore.Audio.Albums.ALBUM));
            textV.setText(myData);

            String[] thumbColumns = {thumb_DATA, thumb_IMAGE_ID};
            CursorLoader thumbCursorLoader = new CursorLoader(
                    myContext, 
                    sourceUri, 
                    null, 
                    null, 
                    null, 
                    null);
            Cursor thumbCursor = thumbCursorLoader.loadInBackground();

            Bitmap myBitmap = null;
            if(thumbCursor.moveToFirst()){
                int thCulumnIndex = thumbCursor.getColumnIndex(thumb_DATA);
                //String thumbPath = thumbCursor.getString(thCulumnIndex);
                String thumbPath = android.provider.MediaStore.Audio.Albums.ALBUM_ART;
                myBitmap = BitmapFactory.decodeFile(thumbPath);
                thumbV.setImageBitmap(myBitmap);
            }
            Animation animation = null;
            animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.wave);
            animation.setDuration(200);
            row.startAnimation(animation);
            animation = null;
            return row;
        }

    }
}
1234567
  • 2,226
  • 4
  • 24
  • 69

1 Answers1

0

Use this,

    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        if(row==null){
            LayoutInflater inflater=getLayoutInflater();
            row=inflater.inflate(R.layout.itemtexview, parent, false);
        }

        ImageView thumbV = (ImageView)row.findViewById(R.id.thumb);
        TextView textV = (TextView)row.findViewById(R.id.text);

        myCursor.moveToPosition(position);

        String myID = myCursor.getString(myCursor.getColumnIndex(android.provider.MediaStore.Audio.Albums._ID));
        String myData = myCursor.getString(myCursor.getColumnIndex(android.provider.MediaStore.Audio.Albums.ALBUM));
        textV.setText(myData);

        // gets the artWorkUri.
        Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");

// gets the albumArtUri from artWorkUri and albumId.
        Uri albumArtUri = Uri.withAppendedPath(sArtworkUri, myID);
        thumbV.setImageURI(albumArtUri);

        Animation animation = null;
        animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.wave);
        animation.setDuration(200);
        row.startAnimation(animation);
        animation = null;
        return row;
    }
Krishna V
  • 1,801
  • 1
  • 14
  • 16
  • if(thumbCursor.moveToFirst()){ //int thCulumnIndex = thumbCursor.getColumnIndex(thumb_DATA); //String thumbPath = thumbCursor.getString(thCulumnIndex); int thumbPath = thumbCursor.getColumnIndexOrThrow(MediaStore.Audio.Albums.ALBUM_ART); myBitmap = BitmapFactory.decodeFile(thumbCursor.getString(thumbPath)); thumbV.setImageBitmap(myBitmap); //} } this gives the first album image set to all the albums if add do all with cursor.moveToNext() then it all becomes too slow , you sollution doesnot work though – 1234567 Jul 03 '15 at 12:28
  • use my code as like this, no need of getting the every time art uri from content uri. check and let me know is working or not. – Krishna V Jul 03 '15 at 16:42