0

ミュージックプレーヤーアプリを作成していて、カーソルを使ってローカルストレージから曲を取得しました。私はアルバムアートと一緒に歌をうまく表示しましたが、スクロールするとハングアップします。この問題を解決するにはどうすればよいですか。アンドロイドでスクロールするとリストビューがハングアップする

これは私のカーソルである:ここでは

cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null,MediaStore.Audio.AudioColumns.DURATION+">0", null, sortOrder); 

は、リストビューのフィールドで曲やアルバムアートをバインドするクラスです。ここ

private class MediaCursorAdapter extends SimpleCursorAdapter { 

    String backgroundColor = "white"; 
    String someOtherBackgroundColor = "#F5F5F5"; 
    public MediaCursorAdapter(Context context, int layout, Cursor c) { 
     super(context, layout, c, 
       new String[]{MediaStore.MediaColumns.DISPLAY_NAME, MediaStore.MediaColumns.TITLE, MediaStore.Audio.AudioColumns.DURATION,MediaStore.Audio.Media.ALBUM_ID}, 
       new int[]{R.id.displayname, R.id.title, R.id.duration,R.id.iv_art}); 
    } 

    public Bitmap getAlbumart(Context context, Long album_id){ 
     Bitmap bm = null; 
     BitmapFactory.Options options = new BitmapFactory.Options(); 
     try{ 
      final Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart"); 
      Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id); 
      ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r"); 
      if (pfd != null){ 
       FileDescriptor fd = pfd.getFileDescriptor(); 
       bm = BitmapFactory.decodeFileDescriptor(fd, null, options); 
       pfd = null; 
       fd = null; 
      } 
     } catch(Error ee){} 
     catch (Exception e) {} 
     return bm; 
    } 

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


     if(cursor.getPosition() % 2 == 0) 
     { 
      view.setBackgroundColor(
        Color.parseColor(backgroundColor)); 
     } 
     else 
     { 
      view.setBackgroundColor(
        Color.parseColor(someOtherBackgroundColor)); 
     } 


     TextView title = (TextView) view.findViewById(R.id.title); 
     TextView name = (TextView) view.findViewById(R.id.displayname); 
     TextView duration = (TextView) view.findViewById(R.id.duration); 
     ImageView iv_art = (ImageView) view.findViewById(R.id.iv_art); 


     String a = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)); 



     Long l = Long.parseLong(a); 

     Bitmap art = getAlbumart(songlist.this,l); 
     if(art!=null) 
     { 
      iv_art.setImageBitmap(art); 
     } 
     else 
     { 
      iv_art.setImageResource(R.mipmap.app_splash_screen_icon); 
     } 

     long durationInMs = Long.parseLong(cursor.getString(
       cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DURATION))); 

     name.setText(cursor.getString(
        cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME))); 

     title.setText(cursor.getString(
        cursor.getColumnIndex(MediaStore.MediaColumns.TITLE))); 

     Utility d = new Utility(); 

     String durationInMin = d.convertDuration(durationInMs); 

     duration.setText("" + durationInMin); 

     view.setTag(cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA))); 
    } 

    @Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent) { 
     LayoutInflater inflater = LayoutInflater.from(context); 
     View v = inflater.inflate(R.layout.listitem, parent, false); 

     bindView(v, context, cursor); 

     return v; 
    } 
} 

は、リストビューのアダプタを設定するコードです:

mediaAdapter = new MediaCursorAdapter(this, R.layout.listitem, cursor); 

lv_songlist.setAdapter(mediaAdapter); 
+0

ビットマップの読み込み中に問題が発生しました。ビットマップをキャッシュにロードします。 –

+0

これを行う方法がわかりません@Dheerubhaiの解決策を教えてください – Pedo

+0

https://github.com/nostra13/Android-Universal-Image-Loaderやその他の画像読み込みライブラリを使用して、imageviewsにビットマップを読み込みます。 –

答えて

1

ListViewに新しい行を作成するたびに、 UIスレッドに格納する必要があります。ストレージからのデータ(特に画像)の読み込みは少し時間がかかりますが、UIスレッドがその画像を少し読み込む以外の作業を実行するのをブロックします。これはあなたが経験する "ぶら下げ"をもたらします。

ベスト・ソリューションは、イメージのロードを別のスレッドにオフロードすることです。これを手動で行っても構いませんし、荒く使用されているイメージローディングライブラリ(Picasso、Glide、UniversalImageLoader)を使って作業することもできます。

+0

おかげさまで@Mauinと私に解決策を教えてください。 – Pedo

+0

ありがとう@Mauin。私はこれを試してみる。 – Pedo

+0

このリンクは利用できません@Mauin。 – Pedo

関連する問題