2017-01-16 8 views
0

albumArtを取得し、カーソルアダプタでAsyncTaskを使用してアートワークをImageViewに設定する際に問題があります。 私は私のアプリを開くと、それは次のようになります。その後、私は戻ってリストをスクロール enter image description hereカーソルアダプタでAsyncTaskを使用してalbumArtをロードする方法

enter image description here

その後、私はリストを下にスクロールし、すべてがうまくている(が、また、私はリストが非常に速くないスクロール場合)非常にbeggining(高速ではありません)にし、それは同様に正常に見える: enter image description here

たぶん私は間違って何かを、私は思うので、私は質問があります。

がする何とかそれは可能ですがその問題を解決するために、AsyncTaskの実装方法をさまざまな方法で試してみましたが、それは正常に動作しますが、終わりには常に同じように見えます。以下は

は、カーソルアダプタのコードです:

public class AllSongsCursorAdapter extends CursorAdapter { 

public AllSongsCursorAdapter(Context context, Cursor cursor) { 
    super(context, cursor, 0); 
} 

public boolean isCheckBoxVisible; 
private AddToFavouritesArray favouritesArray = AddToFavouritesArray.getInstance(); 

private static class ViewHolder { 
    CheckBox checkBox; 
    TextView title; 
    TextView artist; 
    ImageView albumArt; 

    private ViewHolder(View view) { 

     checkBox = (CheckBox) view.findViewById(R.id.favouritesCheckBox); 
     title = (TextView) view.findViewById(R.id.title); 
     artist = (TextView) view.findViewById(R.id.artist); 
     albumArt = (ImageView) view.findViewById(R.id.albumArt); 
    } 
} 

@Override 
public View newView(Context context, Cursor cursor, ViewGroup viewGroup) { 
    View view = LayoutInflater.from(context).inflate(R.layout.song_item, viewGroup, false); 
    ViewHolder viewHolder = new ViewHolder(view); 
    view.setTag(viewHolder); 
    return view; 
} 

@Override 
public void bindView(View view, Context context, final Cursor cursor) { 

    final ViewHolder viewHolder = (ViewHolder) view.getTag(); 
    if (isCheckBoxVisible) { 
     viewHolder.checkBox.setVisibility(View.VISIBLE); 
    } else { 
     viewHolder.checkBox.setVisibility(View.GONE); 
    } 

    final int position = cursor.getPosition(); 
    viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton compoundButton, boolean b) { 

      if (b) { 
       favouritesArray.integerArray.add(position); 

      } else { 
       favouritesArray.integerArray.remove(Integer.valueOf(position)); 
      } 
     } 
    }); 

    int songTitle = cursor.getColumnIndex(MediaStore.Audio.Media.TITLE); 
    int songArtist = cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST); 

    String currentTitle = cursor.getString(songTitle); 
    String currentArtist = cursor.getString(songArtist); 

    viewHolder.title.setText(currentTitle); 
    viewHolder.artist.setText(currentArtist); 

    //__________________________ALBUM_ART_______________________________________________________ 

    viewHolder.albumArt.setTag(cursor.getPosition()); 

    new AsyncTask<ViewHolder, Void, Bitmap>(){ 

     private ViewHolder viewHolder; 

     @Override 
     protected Bitmap doInBackground(ViewHolder... viewHolders) { 
      viewHolder = viewHolders[0]; 
      int id = cursor.getColumnIndex(MediaStore.Audio.Media._ID); 
      long songId = cursor.getLong(id); 

      Bitmap albumArt = getAlbumId(context, songId); 
      return albumArt; 
     } 

     @Override 
     protected void onPostExecute(Bitmap bitmap) { 
      super.onPostExecute(bitmap); 
      if(bitmap != null) { 
       viewHolder.albumArt.setImageBitmap(bitmap); 
      }else{ 
       viewHolder.albumArt.setImageResource(R.drawable.placeholder); 
      } 
     } 
    }.execute(viewHolder); 
} 

private Bitmap getAlbumId(Context context, long id) { 

    Bitmap albumArt = null; 
    String selection = MediaStore.Audio.Media._ID + " = " + id + ""; 
    Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String[]{ 
        MediaStore.Audio.Media._ID, MediaStore.Audio.Media.ALBUM_ID}, 
      selection, null, null); 
    if (cursor.moveToFirst()) { 
     long albumId = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)); 

     albumArt = getAlbumArt(context, albumId); 
    } 
    cursor.close(); 
    return albumArt; 

} 

private Bitmap getAlbumArt(Context context, long albumId) { 

    Bitmap albumArt = null; 
    String selection = MediaStore.Audio.Albums._ID + " = " + albumId + ""; 
    Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, null, selection, null, null); 

    if (cursor.moveToFirst()) { 

     int art = cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART); 

     String currentArt = cursor.getString(art); 
     albumArt = BitmapFactory.decodeFile(currentArt); 
    } 
    cursor.close(); 
    return albumArt; 
} 

}

そして、はい、確かに、AsyncTaskせずにすべてが非常に遅いスクロールを除いて正常に動作します。 ありがとうございます!

EDIT

日の終わりには、それはそれはどのように動作するかです。ご回答ありがとうございました。どうもありがとう。 // __________________________ ALBUM_ART_______________________________________________________

int id = cursor.getColumnIndex(MediaStore.Audio.Media._ID); 
    long songId = cursor.getLong(id); 
    String string = getAlbumArtPath(context, songId); 
    if(string!=null) { 
     Picasso.with(context) 
       .load(new File(string)) 
       .into(viewHolder.albumArt); 
    }else{ 
     viewHolder.albumArt.setImageResource(R.drawable.placeholder); 
    } 
} 
private String getAlbumArtPath(Context context, long id) { 

    String selection = MediaStore.Audio.Media._ID + " = " + id + ""; 
    Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String[]{ 
        MediaStore.Audio.Media._ID, MediaStore.Audio.Media.ALBUM_ID}, 
      selection, null, null); 
    if (cursor.moveToFirst()) { 
     long albumId = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)); 

     return getAlbumArt(context, albumId); 
    } 
    cursor.close(); 
    return null; 

} 

private String getAlbumArt(Context context, long albumId) { 

    String selection = MediaStore.Audio.Albums._ID + " = " + albumId + ""; 
    Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, null, selection, null, null); 

    if (cursor.moveToFirst()) { 

     int art = cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART); 

     return cursor.getString(art); 
    } 
    cursor.close(); 
    return null; 
} 
+0

あなたはこのために画像の読み込みライブラリをチェックしたい場合があります。あなたの正確な問題点をより明確に記述してください。あなたが見たいと思っていないか、何が効いていないのか、何が起こっていますか?あなたのスクリーンショットはこれを非常にきれいにしてはいけません。 –

+0

@ Jonas問題はImageがライトImageViewsに設定されていないことです。たとえば、最初のスクリーンショットと3番目のリストはListViewの非常にうまくできていますが、最初の1つと3番目のアイテムを除くすべてのアイテムに同じAlbumArtセットが表示されることがあります(下にスクロールし、 albumArtsはそれがどのようにすべきかを設定します。 –

答えて

2

あなたは自分で自分のタスクを管理する必要があります。あなたのビューはリサイクルされているので、異なる行に対して1つのビューがあり、問題が発生します。プロジェクトのほとんどはあなたのために自動的にそれを行う、いくつかのライブラリを使用する - はるかに簡単に、例えばPicasso

Picasso.with(context).load(new File(getAlbumArtPath(context, songId))).into(viewHolder.albumArt); 

private String getAlbumArtPath(Context context, long id) { 

    String selection = MediaStore.Audio.Media._ID + " = " + id + ""; 
    Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String[]{ 
        MediaStore.Audio.Media._ID, MediaStore.Audio.Media.ALBUM_ID}, 
      selection, null, null); 
    if (cursor.moveToFirst()) { 
     long albumId = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)); 

     return getAlbumArt(context, albumId); 
    } 
    cursor.close(); 
    return null; 

} 

private String getAlbumArt(Context context, long albumId) { 

    String selection = MediaStore.Audio.Albums._ID + " = " + albumId + ""; 
    Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, null, selection, null, null); 

    if (cursor.moveToFirst()) { 

     int art = cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART); 

     return cursor.getString(art); 
    } 
    cursor.close(); 
    return null; 
} 
+0

@ Orestありがとう、私はすでにピカソをチェックし始めました(リンクありがとう)。すぐに数時間で試してみましょう。ありがとう。 –

関連する問題