【问题标题】:Album art does not show in custom simplecursoradpter专辑封面未显示在自定义 simplecursoradpter 中
【发布时间】:2015-07-04 00:40:04
【问题描述】:

专辑封面不显示在自定义 simplecursoradpter 中

我已成功在列表视图中显示所有专辑名称,但无法显示专辑封面等

虽然文本和动画的一切都很好,但专辑封面却没有出现在列表视图中

这是我正在尝试的代码

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;
        }

    }
}

【问题讨论】:

    标签: android listview albumart


    【解决方案1】:

    用这个,

        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;
        }
    

    【讨论】:

    • if(thumbCursor.moveToFirst()){ //int thClumnIndex = thumbCursor.getColumnIndex(thumb_DATA); //String thumbPath = thumbCursor.getString(thClumnIndex); int thumbPath = thumbCursor.getColumnIndexOrThrow(MediaStore.Audio.Albums.ALBUM_ART); myBitmap = BitmapFactory.decodeFile(thumbCursor.getString(thumbPath)); thumbV.setImageBitmap(myBitmap); //} } 这会将第一张专辑图片设置为所有专辑,如果使用 cursor.moveToNext() 添加全部操作,那么这一切都变得太慢了,尽管您的解决方案不起作用
    • 像这样使用我的代码,不需要每次都从内容 uri 中获取艺术 uri。检查并让我知道是否正常工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-15
    • 1970-01-01
    • 1970-01-01
    • 2016-02-02
    • 1970-01-01
    • 2013-03-15
    相关资源
    最近更新 更多